我已经动态地创建了一些带有一些值的 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];
}
}
我在哪里可以更改以前选择的按钮的图像。
提前致谢