Krunal
Krunal

Reputation: 6490

Unable to implement radio button in iPhone

I am new to iPhone,

I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.

Here is my code snippet,

UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];

UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];

UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];


- (IBAction)RadioButton:(UIButton *)button{

    for (UIButton *Radiobutton in [self.view subviews]) {
        if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
            [Radiobutton setSelected:NO];
        }
    }
    if (!button.selected) {
        button.selected = !button.selected;
    }
}

Any help will be appreciated.

Upvotes: 1

Views: 719

Answers (4)

Accatyyc
Accatyyc

Reputation: 5828

You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.

Change the for loop to

    for (UIButton *Radiobutton in [self.scrollVw subviews]) {

if the scrollVw is added as a property in the .h file.

Upvotes: 2

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

Alternatively you could simply use the iOS provided Control for something like that:

UISegmentedControl

It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.

Upvotes: 1

Priya
Priya

Reputation: 637

You can use following code.

     //Add all the buttons to class level NSMutable array
        UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        Btn1.frame=CGRectMake(10, 190, 20, 20);
        [Btn1 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
        [Btn1 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
        [scrollVw addSubview:Btn1];
        [self.buttonsArray addObject:Btn1];

        UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        Btn2.frame=CGRectMake(10, 240, 20, 20);
        [Btn2 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
        [Btn2 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
        [scrollVw addSubview:Btn2];
        [self.buttonsArray addObject:Btn2];

        UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        Btn3.frame=CGRectMake(10, 290, 20, 20);
        [Btn3 setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
        [Btn3 addTarget:self action:@selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
        [scrollVw addSubview:Btn3];
        [self.buttonsArray addObject:Btn3];

 - (IBAction)RadioButton:(id)sender{

       [self resetAllButtons];
       UIButton* button=(UIButton*) sender;
       [button setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateNormal];

}

 -(void) resetAllButtons{
           for(int i=0;i<[self.buttonsArray count];i++){
              UIButton* button=[self.buttonsArray objectAtIndex:i];
              [button setImage:[UIImage imageNamed:@"radio-off.png"]  forState:UIControlStateNormal];
           }
}

Upvotes: 3

WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html

Upvotes: 1

Related Questions