要保持按钮被选中,您需要在按钮调用的方法中调用 setSelected:YES。例如:
- (void) methodThatYourButtonCalls: (id) sender {
[self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0];
}
- (void) flipButton:(UIButton*) button {
if(button.selected)
[button setSelected:NO];
else
[button setSelected:YES];
}
我知道调用 performSelector: 看起来有点奇怪:而不是仅仅调用 [sender setSelected:YES],但后者对我不起作用,而前者对我有用!
为了在按下不同按钮时取消选择按钮,我建议添加一个实例变量,该变量包含指向当前选定按钮的指针,因此当触摸新按钮时,您可以调用 FlipButton: 相应地取消选择旧按钮。所以现在你的代码应该是:
添加指向您的界面的指针
@interface YourViewController : UIViewController
{
UIButton *currentlySelectedButton;
}
和这些方法到你的实现
- (void) methodThatYourButtonCalls: (id) sender {
UIButton *touchedButton = (UIButton*) sender;
//select the touched button
[self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0];
if(currentlySelectedButton != nil) { //check to see if a button is selected...
[self flipButton:currentlySelectedButton];
currentlySelectedButton = touchedButton;
}
- (void) flipButton:(UIButton*) button {
if(button.selected)
[button setSelected:NO];
else
[button setSelected:YES];
}