iphone - 单击上一个按钮后如何在单选按钮上保持选择

标签 iphone ios uibutton radio-button

对于我的测验应用程序,我通过设置事件和非事件图像为 4 个答案创建 4 个自定义单选按钮。当我选择按钮时,状态变为事件状态,但当我按下前一个按钮时,状态变为非事件状态。

创建自定义按钮的代码

    //answer 1
    targetLabel = [[UILabel alloc] init];
    CGSize labelSize = CGSizeMake(240, 9999);
    CGSize theStringSize = [ans1 sizeWithFont:targetLabel.font          constrainedToSize:labelSize lineBreakMode:targetLabel.lineBreakMode];
    targetLabel.frame = CGRectMake(targetLabel0.frame.origin.x+40, targetLabel0.frame.size.height+40, theStringSize.width, theStringSize.height);
    targetLabel.text = ans1;
    [targetLabel setNumberOfLines:0];
    targetLabel.backgroundColor=[UIColor clearColor];
    [targetLabel sizeToFit];
    [scroll addSubview:targetLabel];
    // button 1
 ans1btn = [[UIButton alloc] initWithFrame:CGRectMake(15,targetLabel.frame.origin.y-5,30,30)];
    ans1btn.backgroundColor=[UIColor redColor];
     [ans1btn setSelected:NO];
    [ans1btn setBackgroundImage:[UIImage imageNamed:@"gray.png"]
                       forState:UIControlStateNormal];
    [ans1btn setBackgroundImage:[UIImage imageNamed:@"green.png"]
                       forState:UIControlStateSelected];
    ans1btn.adjustsImageWhenHighlighted=YES;
    [ans1btn addTarget:self
                action:@selector(checkboxSelected:)
      forControlEvents:UIControlEventTouchUpInside];
    ans1btn.tag=1;
    [scroll addSubview:ans1btn];

调用方法

-(void)checkboxSelected:(id)sender
{
checkBoxSelected = TRUE;
[ans1btn setSelected:checkBoxSelected];
if(checkBoxSelected==TRUE)
{
    checkBoxSelected1=FALSE;
    [ans1btn1 setSelected:checkBoxSelected1];
    [ans1btn2 setSelected:checkBoxSelected1];
    [ans1btn3 setSelected:checkBoxSelected1];
    checkBoxSelected=TRUE;
    targetLabel.textColor=[UIColor blackColor];
    targetLabel1.textColor=[UIColor grayColor];
    targetLabel2.textColor=[UIColor grayColor];
    targetLabel3.textColor=[UIColor grayColor];
}
else
{
    NSLog(@"the button is not selected");
}
}

就像我也调用其他按钮操作一样

谁能帮帮我

提前致谢。

最佳答案

iOS 中没有实际的单选按钮,但您可以使用以下代码来执行您想要的操作,

First、second、third 和fourthButtonTags 是已经在.xib 或.m 文件中设置的值。也可以使用特定数字。

在你的.h文件中;

@property (strong, nonatomic) UIButton *firstButton, *secondButton, *thirdButton, *fourthButton;

在您的 .m 文件中;

- (IBAction)buttonSelection:(id)sender {
    UIButton *senderButton = (UIButton *) sender;

    if (senderButton.tag == firstButtonTag) {
        if (_firstButton.selected) {
            _firstButton.selected = NO;
        }
        else {
            _firstButton.selected = YES;
            _secondButton.selected = NO;
            _thirdButton.selected = NO;
            _fourthButton.selected = NO;
        }
    }
    else if (senderButton.tag == secondButtonTag) {
        if (_secondButton.selected) {
            _secondButton.selected = NO;
        }
        else {
            _secondButton.selected = YES;
            _firstButton.selected = NO;
            _thirdButton.selected = NO;
            _fourthButton.selected = NO;
        }
    }
    else if (senderButton.tag == thirdButtonTag) {
        if (_thirdButton.selected) {
            _thirdButton.selected = NO;
        }
        else {
            _thirdButton.selected = YES;
            _firstButton.selected = NO;
            _secondButton.selected = NO;
            _fourthButton.selected = NO;
        }
    }
    else if (senderButton.tag == fourthButtonTag) {
        if (_fourthButton.selected) {
            _fourthButton.selected = NO;
        }
        else {
            _fourthButton.selected = YES;
            _firstButton.selected = NO;
            _secondButton.selected = NO;
            _thirdButton.selected = NO;
        }
    }
}

关于iphone - 单击上一个按钮后如何在单选按钮上保持选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019698/

相关文章:

ios - 捕捉使用 CoreGraphics 绘制的形状内的触摸 - Objective C

iphone - viewDidAppear 方法在 locationManager 可以定位用户之前调用

iPhone 操作按钮图像文件随处可用?

iphone - subview 外的 UIButton 可点击区域

ios - UICollectionView 在更改布局时显示旧单元格

iphone - 如何从 JSON 中获取数据?

iphone - 需要帮助呈现 View Controller

iphone - 电影下载器应用程序如何在 iOS 中运行?

ios - 无法通过 iVar/property 访问 UIButton

ios - 按钮点击事件不触发