Reputation:
i am using radio button image (empty circle) in button to answer the question from 3 options, and the 3 options are radio button's. i have created 3 uibuttons programmatically in tableview delegate method cellforrowatindexpath. i need when one button is selected(with filled circle image) other one if selected before gets unselected. i am using below code in button clicked method.
static int _row;
-(IBAction) optionClicked:(id)sender { UIButton **btn = (UIButton*)sender; _row = btn.tag;
if (btn.tag == 0)
{
if(btn1On)
{
[btnrad1 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
btn1On=FALSE;
}
else
{
[btnrad1 setImage:[UIImage imageNamed:@"filled.png"]forState:UIControlStateNormal];
btn1On = TRUE;
btn2On = FALSE;
btn3On = FALSE;
}
}
else if (btn.tag == 1)
{
if(btn2On)
{
[btnrad2 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
btn2On=FALSE;
}
else
{
[btnrad2 setImage:[UIImage imageNamed:@"filled.png"]forState:UIControlStateNormal];
btn2On = TRUE;
btn1On = FALSE;
btn3On = FALSE;
}
}
else if (btn.tag == 2)
{
if(btn3On)
{
[btnrad3 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
btn3On=FALSE;
}
else
{
[btnrad3 setImage:[UIImage imageNamed:@"filled.png"]forState:UIControlStateNormal];
btn3On = TRUE;
btn1On = FALSE;
btn2On = FALSE;
}
}
else
{
NSLog(@"Error");
}
}
above code is doing selection of button in another row. like i am selecting 1st option in 1st row but its is selecting 2nd row button. i don't know how to use _row to check for every cell of table view.
Upvotes: 0
Views: 3046
Reputation: 39376
I haven't tried this, but here goes:
Make a special view for all our radio buttons. Any buttons or controls in that view are now considered linked together. Put indexPath.row in all this view's tag.
Then add a touchDown or touchUpInside method to your buttons so that when the button is hit, you are called to handle it. Like what you have. Make sure this method is in your UITableViewCell implementation.
Then when the button is hit, get
row = sender.superview.tag; or row = [[sender superview] tag];
Now you know which row's buttons you are dealing with. Now you need to get a list of all the buttons in the view, so you can manipulate them, and you do that with
buttons = [[sender superview] subviews];
buttons will now be an array of your radio buttons. These are all the radio buttons in your cell. Turn them all off using your:
for ((UIButton *) button in buttons)
[button setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
Then turn on the button that was hit:
[sender setImage:[UIImage imageNamed:@"filled.png"]forState:UIControlStateNormal];
That one's being passed to you in sender. Update your internal state settings, using row, to let you know which radio button is now on and which are off.
Upvotes: 1
Reputation: 95315
As mahboudz pointed out, I now understand you are actually having issues with buttons in different rows being selected.
This may be because the variables btnrad1
, btnrad2
, and btnrad3
are being overwritten by each subsequent call of -cellForRowAtIndexPath:
. Even though each button is distinctly allocated, you only have references to the last three of the radio buttons that you created.
You need to come up with a way to maintain references to each radio button that you create. For example, you could use an NSMutableArray
containing child arrays containing your buttons. The indexes to the outer array represent the rows in your table view and the indexes to the child arrays represent the individual radio buttons.
Upvotes: 0