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;
     }
}

good luck!

Comments are closed.