Radio buttons for iPhone application
Categories: iPhone on Feb.03, 2010
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!
Similar posts:

April 29th, 2010 on 12:52 pm
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
July 14th, 2010 on 2:27 pm
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….
September 5th, 2011 on 12:24 pm
[...] implementing their own UIButton derivates to create radio buttons on iPhone, as this blog post and this one [...]