自定义单选按钮一次为iPhone选择一个?

时间:2012-07-03 05:12:00

标签: iphone radio-button state selected

我已经动态创建了一些带有一些值的RadioButtons列表。以编程方式我更改了按钮状态并更改了选定和未选中的图像。但问题是我可以同时选择所有的radioButtons。实际上我需要一次选择一个。 当我点击下一个RadioButton时,之前选择的按钮状态应该更改为未选中。

这是我的代码,我尝试更改图片,但是......我的代码存在一些问题。

   RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
   [RadioButton setCenter:CGPointMake(116.0,p1)];
   [RadioButton setSelected:NO];
   [RadioButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
   [RadioButton addTarget:self action:@selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
   [self.scrollView addSubview:RadioButton];




  -(void)RadioButtonTapped:(id)sender
    {


       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]; //not working, button image is not changing 
        } 
     else
        { 
            [Button setSelected:NO]; 
            [Button setImage:[UIImage imageNamed:@"radio_inactive.png"] forState:UIControlStateNormal];
        }


 }

我在哪里可以更改以前所选按钮的图像。

提前致谢

4 个答案:

答案 0 :(得分:1)

选择一个按钮时取消选择所有按钮。如果滚动视图中有按钮,则可以使用以下代码:

//Your Method
-(void)RadioButtonTapped:(id)sender
{
  UIButton *RadioButton1 = (UIButton*)sender;
  [self deselectAll];
  [self radiobuttonAction:RadioButton1];      
}

- (void) deselectAll : (UIScrollView *) scrollView{
  NSArray *viewArray = [scrollView subviews];
  for (UIView *v in viewArray){
  if([v isKindOfClass:[UIButton class]]){
        [((UIButton *)v) setSelected:NO];
  }
 }
}

编辑:但是如果你想给它真正的单选按钮效果(总是选择一个,只选择一个),你会更容易。使用以下代码:

 //A globle refButton
  UIButton *refButton = nil;
 //Set image for both state:
[RadioButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
[RadioButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateSelected];

 //make any of above default select: may be the last one and pass that to `refButton`
 refButton = RadioButton;



 //Your Method
-(void)RadioButtonTapped:(id)sender
{
  [refButton setSelected:NO];
  refButton = (UIButton*)sender;
  [refButton setSelected:YES];      
}

答案 1 :(得分:0)

在创建时标记按钮。 按下按钮时,通过标签获取按钮。取消选择所有按钮。更新您作为n参数收到的当前按钮。

答案 2 :(得分:0)

假设您要添加5个单选按钮。

 - (void)viewDidLoad
 {
      for (int i = 0; i < 5; i++) {
           UIButton*  RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
           [RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
           [RadioButton setCenter:CGPointMake(116.0, i * 40)];
           //        [RadioButton setTag:i * 10];
           [RadioButton setSelected:NO];
           [RadioButton setImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal];
           [RadioButton addTarget:self action:@selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
           [self.scrollView addSubview:RadioButton];

     }
  }

//do not forget to declared this in your header file
-(void)RadioButtonTapped:(UIButton*)button;
 {
       for (UIButton *btn in self.scrollView.subviews) {
          [btn setImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal];
         }
       [button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
 }

上面的代码更新了用户界面,将所有按钮的图像设置为“取消选中”,然后将最后按下的按钮的图像更改为“检查”。

答案 3 :(得分:-2)

创建这样的按钮,

for(int i=0;i<4;i++)
{
 RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
 RadioButton.tag = i*100;
 [RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
 [RadioButton setCenter:CGPointMake(116.0,p1)];
 [RadioButton setSelected:NO];
 [RadioButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
 [RadioButton addTarget:self action:@selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
 [self.scrollView addSubview:RadioButton];

}

添加这样的动作,

-(void)radiobuttonAction:(UIButton *)Button
{

   UIButton *Button = (UIButton*)sender;
   for(UIButton * btn in self.scrollview.subViews)
   {
     if(btn.tag == Button.tag)
     {
        if(![Button isSelected])
        {
          [Button setSelected:YES]; 
          [Button setImage:[UIImage imageNamed:@"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing 
        } 
        else
        { 
          [Button setSelected:NO]; 
          [Button setImage:[UIImage imageNamed:@"radio_inactive.png"] forState:UIControlStateNormal];
        }
     }
     else
    {
      //do selected or de-selected code for other buttons

    }

}