-3

在我的应用程序中,我有 3 个按钮,我想在选择任何 1 个按钮时禁用剩下的 2 个按钮,我尝试使用以下逻辑,这里我用来执行它的代码无法正常工作,

 -(void)checkboxSelected13:(id)sender
 {
       UIButton *btn = (UIButton *) sender;
       if([btn isSelected])
       {
            [bt27 setSelected:YES];
            [bt28 setSelected:NO];
            [bt29 setSelected:NO];
       }
       else if ([btn isSelected]) {
            [bt27 setSelected:NO];
            [bt28 setSelected:YES];
            [bt29 setSelected:NO];
       }
       else
       {
            [bt27 setSelected:NO];
            [bt28 setSelected:NO];
            [bt29 setSelected:YES];
       }
}

我的代码有什么问题?请帮忙

4

6 回答 6

3

您正在两个 if-else 部分中检查相同的条件“if([btn isSelected])”。这可能会引起混乱。而且您的代码无法正常工作。

而且您的代码看起来太长而且很复杂:

您可以直接执行:

为您的按钮提供标签属性,例如 button1=1、button2=2 和 button3=3

现在检查以下代码:

-(IBAction)checkboxSelected:(id)sender
{
    UIButton *btn = (UIButton *) sender;

    switch (btn.tag) {
    case 1:
        [btn1 setSelected:YES];
        [btn2 setSelected:NO];
        [btn3 setSelected:NO];
        break;
    case 2:
        [btn1 setSelected:NO];
        [btn2 setSelected:YES];
        [btn3 setSelected:NO];
        break;
    case 3:
        [btn1 setSelected:NO];
        [btn2 setSelected:NO];
        [btn3 setSelected:YES];
        break;
    default:
        [btn1 setSelected:NO];
        [btn2 setSelected:NO];
        [btn3 setSelected:NO];
        break;
    }
}

希望,这将对您有所帮助。

干杯!

于 2013-02-12T10:35:34.020 回答
1

您应该这样做,并将不同的图像作为背景图像作为 xib 文件中按钮的选定状态的背景图像,以便可以轻松区分

-(void)checkboxSelected13:(id)sender
 {

UIButton *btn = (UIButton *) sender;

if((btn == bt27) && ![btn isSelected])
{
    [bt27 setSelected:YES];
    [bt28 setSelected:NO];
    [bt29 setSelected:NO];
}
else if ((btn == bt28) && ![btn isSelected]) {
    [bt27 setSelected:NO];
    [bt28 setSelected:YES];
    [bt29 setSelected:NO];
}
else if ((btn == bt29) && ![btn isSelected])
{
    [bt27 setSelected:NO];
    [bt28 setSelected:NO];
    [bt29 setSelected:YES];

}
}
于 2013-02-12T10:31:53.683 回答
1

将选项卡分配给您的按钮。然后检查:

-(void)checkboxSelected13:(id)sender
 {
      UIButton *btn = (UIButton *) sender;
      if(btn.tag == tagAssignedToButton27)
      {
           [bt27 setSelected:YES];
           [bt28 setSelected:NO];
           [bt29 setSelected:NO];
      }
      else if(btn.tag == tagAssignedToButton28)
      {
           [bt27 setSelected:NO];
           [bt28 setSelected:YES];
           [bt29 setSelected:NO];
      }
      else if(btn.tag == tagAssignedToButton29)
      {
           [bt27 setSelected:NO];
           [bt28 setSelected:NO];
           [bt29 setSelected:YES];
      }
}

更优化的方法是在你的函数中使用 switch case checkboxSelected13

于 2013-02-12T10:31:57.553 回答
1
if([bt27 isSelected]) {
    [bt27 setSelected:YES];
    [bt28 setSelected:NO];
    [bt29 setSelected:NO];
} else if ([bt28 isSelected]) {
    [bt27 setSelected:NO];
    [bt28 setSelected:YES];
    [bt29 setSelected:NO];
} else if ([bt29 isSelected]) {
    [bt27 setSelected:NO];
    [bt28 setSelected:NO];
    [bt29 setSelected:YES];
} else {
    [bt27 setSelected:NO];
    [bt28 setSelected:NO];
    [bt29 setSelected:NO];
}

从逻辑上讲,这就是你想要的。目前还不清楚 btn 和 btXY 之间的关系。

于 2013-02-12T10:32:32.633 回答
0
if([btn isSelected]
   )
{
    [bt27 setSelected:YES];
    [bt28 setSelected:NO];
    [bt29 setSelected:NO];
}
else if ([btn isSelected]) {
    [bt27 setSelected:NO];
    [bt28 setSelected:YES];
    [bt29 setSelected:NO];
}
...

您在 if 和 else if 中检查相同的条件 [btn isSelected],更改其中一个。

于 2013-02-12T10:27:53.630 回答
0

好吧,这里的主要问题是逻辑和代码重复。尽管另一种类型的控件最适合您所追求的(UISegmentedControl例如),但您可以尝试用更抽象的想法来解决相同的问题,也许像这样(请注意,所有按钮都会触发相同的方法现在):

- (IBAction)checkboxSelected:(id)sender
{
    // Deselect any button that is already selected
    // of course a better approach would be to keep
    // an array with the buttons and just iterate through
    [bt27 setSelected:NO];
    [bt28 setSelected:NO];
    [bt29 setSelected:NO];

    // Select the button that triggered the action
    [(UIButton *)sender setSelected:YES];
}
于 2013-02-12T10:34:18.900 回答