3按钮选择

时间:2013-02-12 10:23:32

标签: iphone ios6 uibutton

在我的应用程序中,我有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];
       }
}

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

6 个答案:

答案 0 :(得分: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;
    }
}

希望,这对你有所帮助。

干杯!

答案 1 :(得分: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];

}
}

答案 2 :(得分: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];
      }
}

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

答案 3 :(得分: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是如何联系的。

答案 4 :(得分: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],改变其中一个条件。

答案 5 :(得分: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];
}