views:

644

answers:

3

hi, 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.

A: 

Edit

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.

dreamlax
I think he has buttons in a UITableView and his problem was that when he manipulates buttons in one cell, buttons in another cell were turning on and off.
mahboudz
Ohhhhhh, it sounds like the references to btnrad[1-3] are being overwritten by each call to `-cellForRowAtIndexPath:`
dreamlax
I read the question and thought button 2 was being turned on when button 1 was clicked.
dreamlax
hi mahboudz, you are absolutely correct. do you have any solution for this problem
hi dreamlax, could you provide some code for this problem.
A: 

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.

mahboudz
hi mahboudz, could u tell me how to grab the UIbutton's superview
To make it easier, put all you buttons in a view, and tag that view with the indexPath.row. Now when you get a button tap, look at the superview, and get its tag, and then use superview.subviews which will give you an array of buttons. You now have all the buttons to use as above.
mahboudz
I edited the answer to use the new method.
mahboudz
A: 

hi mahboudz, i have created 3 uibutton in cellforrowatindexpath like below

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
btnrad1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnrad1.frame = CGRectMake(5,66,10,10);
    btnrad1.tag = indexPath.row;
    [btnrad1 addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [btnrad1 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    [btnrad1 addTarget:self action:@selector(optionClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnrad1];
    [cell.contentView addSubview:value];
    btnrad1.tag = indexPath.row; }

& below is d method

-(IBAction)optionClicked:(id)sender
{
    UIButton **btn2= (UIButton*)sender;
    _selectedRow1 = btn2.tag;
    if(btn1On)
    {
    [btnrad1 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    [btnrad2 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    [btnrad3 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    btn1On=FALSE;
    }
    else
    {
    [btnrad1 setImage:[UIImage imageNamed:@"filled.png"]forState:UIControlStateNormal];
    [btnrad2 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    [btnrad3 setImage:[UIImage imageNamed:@"unfilled.png"]forState:UIControlStateNormal];
    btn1On = TRUE;
    btn2On = FALSE;
    btn3On = FALSE;
    }
}

tell me where am i wrong & correct me. actually am confused from your code row = sender.superview.tag; or row = [[sender superview] tag]; it's giving error.

anyone has any solution 2 my problem