在iOS中仅设置单个选项

时间:2015-10-26 05:45:15

标签: ios objective-c

我有两个自定义圆形矩形按钮,并在此图像显示上点击事件。喜欢当我们选择性别时,只有一个选项可以选择其他自动隐藏。但是当我触摸第一个按钮而不是它选择但是当我触摸第二个按钮无操作选择。 我的主要动机是我想选择性别但在iOS中没有单选按钮使用所以我设计自定义和点击事件集图像。 调用api是一种字符串类型。

        -(void)viewdidload
        {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            callApi=@"MALE";//calling Api is a string type//
            [button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        }

        -(void)roundButtonDidTap:(UIButton*)tappedButton
         {
            if([callApi isEqualToString:@"FEMALE"]) {
               UIImage *btnImage = [UIImage imageNamed:@"bullet.png"];
               [button setImage:btnImage forState:UIControlStateNormal];                             
            }

            if ([callApi isEqualToString:@"MALE"]) {                          
               UIImage *btnImage = [UIImage imageNamed:@"bullet.png"];
               [button2 setImage:btnImage forState:UIControlStateSelected];
               button.imageView.hidden=YES;             
            }
         }

1 个答案:

答案 0 :(得分:0)

试试这段代码:

        -(IBAction)selectMale:(id) sender{ // MALE BUTTON
        UIButton *RadioButton1 = (UIButton*)sender;
        [self radiobuttonAction:RadioButton1];
        }

        -(void)radiobuttonAction:(UIButton *)Button
        {
        if(![Button isSelected])
        {
            [Button setSelected:YES];
            [Button setImage:[UIImage imageNamed:@"radio_active.png"] forState:UIControlStateSelected];
            gender = @"Male";
            [btnFemale setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateSelected];
            [btnFemale setSelected:NO];

        }
        else
        {
            [Button setSelected:NO];
            [Button setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateNormal];
        }
         }

        -(IBAction)selectFemale:(id) sender{
        UIButton *RadioButton1 = (UIButton*)sender;
        [self radiobuttonActionFemale:RadioButton1];
         }
        -(void)radiobuttonActionFemale:(UIButton *)Button
        {
        if(![Button isSelected])
        {
            [Button setSelected:YES];
            [Button setImage:[UIImage imageNamed:@"radio_active.png"] forState:UIControlStateSelected];
            gender = @"Female";
            [btnMale setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateSelected];
          [btnMale setSelected:NO];
        }
        else
        {