how implement radio button behavior?
I have two UIButtons. they behave like check boxes(both buttons can select at a time). But I want only one 开发者_运维百科button can select at a time (like radio buttons). How can I do it? This how I implement checkbox button feature.
if ([btnAnswer1 isSelected]) {
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
[btnAnswer1 setSelected:NO];
}else {
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[btnAnswer1 setSelected:YES];
}
if ([btnAnswer2 isSelected]) {
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
[btnAnswer2 setSelected:NO];
}else {
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
[btnAnswer2 setSelected:YES];
}
The way I usually implement radio buttons is:
-(void) radioButtonWasSelected:(RadioButton*) which
{
//set all radio buttons to unselected
//set 'which' to selected
//do something with 'which'
}
Though I haven't check, just try with given modified code.
if ([btnAnswer1 isSelected])
{
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState:UIControlStateNormal];
[btnAnswer1 setSelected:NO];
selected = FALSE;
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState :UIControlStateSelected];
[btnAnswer2 setSelected:YES];
}
else
{
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateSelected];
[btnAnswer1 setSelected:YES];
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState: UIControlStateNormal];
[btnAnswer2 setSelected:NO];
selected = TRUE;
}
if ([btnAnswer2 isSelected])
{
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState: UIControlStateNormal];
[btnAnswer2 setSelected:NO];
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateSelected];
[btnAnswer1 setSelected:YES];
}
else
{
[btnAnswer2 setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateSelected];
[btnAnswer2 setSelected:YES];
[btnAnswer1 setBackgroundImage:[UIImage imageNamed:@"checkbox_unchecked.png"] forState: UIControlStateNormal];
[btnAnswer1 setSelected:NO];
}
精彩评论