UI按钮选择状态

时间:2011-02-19 16:34:17

标签: xcode button uibutton selected

我正在制作一个包含20个问题的应用程序,您可以使用每个问题的按钮回答“是”或“否”。所以我使用了40个按钮。 当我按下YES按钮时它应该保持选中状态(有一个默认图像和所选图像),如果我点击NO它应该保持选中状态并取消选择YES按钮。 这是我正在使用的代码。它适用于2个按钮,但它不适用于更多按钮。

·H

IBOutlet UIButton *bot2;
IBOutlet UIButton *bot3;
IBOutlet UIButton *bot4;
IBOutlet UIButton *bot5;
总共40个 }

-(IBAction)a:(id)sender;
-(IBAction)b:(id)sender;
-(IBAction)c:(id)sender;
-(IBAction)d:(id)sender;
-(IBAction)e:(id)sender;
-(IBAction)f:(id)sender;
-(IBAction)g:(id)sender;
-(IBAction)h:(id)sender;
-(IBAction)i:(id)sender;

.....总共40个

@end

的.m

-(IBAction)a:(id)sender
{
    if (ButtonSelected == 0)
    {
    [bot2 setSelected:YES];
    ButtonSelected = 1;

    }
    else
    {
    [bot2 setSelected:NO];
            ButtonSelected = 0;
    }
}


-(IBAction)b:(id)sender
{
    if (ButtonSelected == 0)
    {
        [bot3 setSelected:YES];
        ButtonSelected = 1;
    }
    else
    {
        [bot3 setSelected:NO];
        ButtonSelected = 0;
    }
}
-(IBAction)c:(id)sender
{
    if (ButtonSelected == 0)
    {
        [bot4 setSelected:YES];
        ButtonSelected = 1;
    }
    else
    {
        [bot4 setSelected:NO];
        ButtonSelected = 0;
    }
}

ecc eccc ......

1 个答案:

答案 0 :(得分:2)

对于这种情况,最好为每个是/否组合使用UISegmentedControl实例。