1

对于我的测验应用程序,我通过设置活动和非活动图像为 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");
}
}

像这样我也在调用其他按钮动作

谁能帮帮我吗。

提前致谢。

4

2 回答 2

1

iOS 中没有实际的单选按钮,但您可以使用以下代码来做您想做的事,

第一个、第二个、第三个和第四个ButtonTags 是已经在.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;
        }
    }
}
于 2013-06-10T08:59:48.377 回答
1

像这样尝试,在这里全局定义一个按钮并使用该按钮进行播放,无需使用标签和所有其他东西

@property(nonatomic,retain)UIButton *previousSelectedBtn;

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

}
于 2013-06-10T09:07:48.817 回答