xcode UIbutton选中状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5051995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:16:10  来源:igfitidea点击:

UIbutton selected state

xcodebuttonuibuttonselected

提问by Marco

I am making an app with 20 questions, and you can answer Yes or No with the buttons for each questions. so I using 40 buttons. when I clic the YES button it should remain selected (there is a default image and selected image), if I click NO it should remain selected and deselected the YESbutton. this is the code i am using. it works with 2 buttons, but it does not work with more buttons.

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

.h

。H

IBOutlet UIButton *bot2;
IBOutlet UIButton *bot3;
IBOutlet UIButton *bot4;
IBOutlet UIButton *bot5;

40in total }

共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 in total

.....40 共

@end

.m

.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......

ecc eccc……

回答by Joubert Nel

For this scenario, it is probably better to use instances of UISegmentedControl for each Yes/No combo.

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