Home » iPhone » Radio buttons for iPhone application

Radio buttons for iPhone application

Presented simple and nice solution for radio buttons, based on the UIButton.

Add buttons on view of controller

1
2
3
4
5
6
7
8
9
for (int i = 0; i < 5; i++) {
          UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
          [but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
          [but setImage:[UIImage imageNamed:@"checkedbox.png"] forState:UIControlStateSelected];
          [but setFrame:CGRectMake(0, 0, 17, 17)];
          [but setCenter:CGPointMake( 50,  i*40+20 )];
          [but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
          [self.view addSubview:but];
     }


Action for buttons:

1
2
3
4
5
6
7
8
9
10
11
- (IBAction)checkboxButton:(UIButton *)button{
     
     for (UIButton *but in [self.view subviews]) {
          if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
               [but setSelected:NO];
          }
     }
     if (!button.selected) {
          button.selected = !button.selected;
     }
}

you can get UI from here

good luck!

6 thoughts on “Radio buttons for iPhone application

  1. Sir,
    i took your code in my app. i put the First code in “-(void)ViewDidLoad” method & i put the next code in -(IBAction) as you have given. but i am not getting any output on my app. my app dosen’t give any error but how i call IBAction. I thought you have added buttons through coding in first section. do i have to add buttons to my app again to call the IBAction. please give me your help so that i can complete this task. I am desparately need this soluiton. Consider my foolishness & please help me
    Mahesh

  2. HI

    This code working Good in My Application But if I want to Get Those radio button vales Like I was given Two labels For two radio buttons so, How can i get Values? Please give Your Valuable response…. so i can able to Move forward in My Project.. Please send your Reply….

  3. Pingback: My Homepage
  4. hello sir,
    this is working good
    but i have a page in that two radio group and each of them one is selected compulsory radio button .
    it mean that one selection for group A and other is group B in same page…please help me….

Comments are closed.