它不起作用,因为您只需按一下按钮就会收到通知。例如,如果您按下标签为 0 的按钮,则将buttonPressed:
使用按钮 0 作为参数调用该方法。您正在测试是否在 if 表达式中选择了两次按钮:button.selected
。但这两次都只会检查最近按下的按钮。
if ((button.tag==0 && button.selected) || (button.tag==12 && button.selected))
您必须独立测试每个按钮状态...例如,您可以创建两个存储指向按钮的指针的出口变量。把它放在你的视图控制器头文件中:
@property (weak, nonatomic) IBOutlet UIButton *button0;
@property (weak, nonatomic) IBOutlet UIButton *button12;
然后使用布局设计器将您的插座连接到这些按钮。之后,您可以访问其中任何一个的 .selected 属性。
- (IBAction)buttonPressed:(UIButton *)button {
button.selected = !button.selected; // i'm not sure of this line
if ((button.tag==0 || button.tag==12)
{
if( button0.selected || button12.selected )
{ // one of them is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"1"];
} else { // none is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"0"];
}
}
}
或者,如果您想避开这些渠道,请查看此问题。您可以通过其标签获得一个视图,例如一些 UIButton。如果您的视图控制器中有视图属性,您可以编写如下内容:
- (IBAction)buttonPressed:(UIButton *)button {
button.selected = !button.selected; // i'm not sure of this line
if ((button.tag==0 || button.tag==12)
{
UIButton *button0 = (UIButton *)[self.view viewWithTag:0];
UIButton *button12 = (UIButton *)[self.view viewWithTag:12];
if( button0.selected || button12.selected )
{ // one of them is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"1"];
} else { // none is selected
NSRange range = {0,1};
[buttonPressings replaceCharactersInRange:range withString:@"0"];
}
}
}