0

我已经动态地创建了一些带有一些值的 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

4 回答 4

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];      
}
于 2012-07-03T05:39:42.970 回答
0

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

于 2012-07-03T05:22:47.823 回答
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];
 }

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

于 2012-07-03T05:38:37.637 回答
-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

    }

}
于 2012-07-03T05:18:51.950 回答