user6183984
user6183984

Reputation: 75

Dynamic Radio Button in Objective C

I want to design a dynamic radio button in my app

 for (int f = 0; f<arr.count; f++) {
 UILabel *lbl = [[UILabel alloc]init];
 lbl.frame = CGRectMake(radio_x+10,radio_y+5 , radio_w, radio_h);
 lbl.text = arr[f];
 lbl.textColor = [UIColor blackColor];                    
 [self.sub_View addSubview:lbl];
 self.yourButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosOfTxt,radio_y , 20, 20)];
 [self.yourButton setImage: [UIImage imageNamed:@"RadioButton-Selected.png"]forState:UIControlStateNormal];
 [self.yourButton setImage: [UIImage imageNamed:@"RadioButton-Unselected.png"]forState: UIControlStateSelected];
 [self.yourButton setTag:baseRadioTag+f];
 [self.sub_View addSubview:self.yourButton];
 [self.yourButton addTarget:self action:@selector(radioSelected:) forControlEvents:UIControlEventTouchUpInside];
 }

-(void)radioSelected:(UIButton*)sender {
 self.yourButton.selected = false;
sender.selected = !sender.selected;
self.yourButton = sender;    }

I did like this , this is working like , selecting all the options , i want to select only one option at once , if I selected option 1 -> option 2 should be unselected . If I selected option 2 -> option 1 should be deselected.Please help me to do this .

Upvotes: 1

Views: 846

Answers (1)

Vikky
Vikky

Reputation: 936

First of all to make things easier

create a array which can hold reference of all the buttons

@interface ViewController ()
{
NSMutableArray *btnArray;
}

Create an object of Array

- (void)viewDidLoad {
[super viewDidLoad];
btnArray = [NSMutableArray new];
/// Here is your code for creating buttons
}

Now add all created buttons to this array

// this is last line from your code where you create your UI
[self.yourButton addTarget:self action:@selector(radioSelected:) forControlEvents:UIControlEventTouchUpInside];
// add this line to add your button to array
[btnArray addObject:self.yourButton];
 }

Now when you click button radioSelected method is called

NOTE -: Here can be two cases according to me, but i don't know which one you require so i am explaining both of them

CASE FIRST :-When you select or deselect buttons final output can be that there can be no button selected(all can be deselected)

 -(void)radioSelected:(UIButton*)sender{
// here if button is selected, deselect it otherwise select it
[sender setSelected:!sender.isSelected];
for (UIButton* btn in btnArray) {
    // here we mark all other button as deselected except the current button

 if(btn.tag != sender.tag){
        // here when you deselect all button you can add a if statement to check that we have to deselect button only if it is selected
        if(btn.isSelected){
            [btn setSelected:NO];
        }
    }
}
}

CASE SECOND:-When atleast one button will be selected

 -(void)radioSelected:(UIButton*)sender{
// here if current button is already selected we will not allow to deselect it and return
if(sender.isSelected){
    return;
}
// here working is as same as described above
[sender setSelected:!sender.isSelected];
for (UIButton* btn in btnArray) {
    if(btn.tag != sender.tag){
        [btn setSelected:NO];
    }
}
}

You can test both cases and use according to your requirement.

Hope this works for you.

And let me know if you face any issues :)

Upvotes: 1

Related Questions