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

时间:2013-06-10 08:27:03

标签: 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");
}
}

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

任何人都可以帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:1)

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;
        }
    }
}

答案 1 :(得分:1)

尝试这样,在这里全局定义一个按钮并使用该按钮,不需要带标签和所有其他东西

@property(nonatomic,retain)UIButton *previousSelectedBtn;

- (IBAction)radioButtonSelection:(UIButton *)sender {
    sender.selected=YES;
    if(_previousSelectedBtn)
        _previousSelectedBtn.selected=NO;
    _previousSelectedBtn=sender;

}