50

I would like to ask if there are examples out there on how to implement radio-button options on an iPhone app.

I find the Picker View quite big for a simple selection feature.

I'm not sure if Apple excluded radio buttons on purpose, and whether if it is better to simply use a Picker View from a usability / user experience point-of-view.

Duck
  • 34,902
  • 47
  • 248
  • 470
Jonas Arcangel
  • 2,085
  • 11
  • 55
  • 85
  • 7
    I think it's made big on purpose. Safari on iPhone uses the picker view for selection boxes, for example. I recommend you to follow what's standard (the picker view in this case) on the device you're developing for, not what you got accustomed to (radio buttons in this case) on the other machines you're working with so far. – Yuji Jan 31 '11 at 03:17
  • Check out this great tutorial [iPhone UIButton tutorial: Radio Buttons](http://www.mobisoftinfotech.com/blog/iphone/iphone-uibutton-tutorial-radio-buttons/). – WrightsCS Jan 31 '11 at 03:12

8 Answers8

42

I have some thoughts on how the best radio button implementation should look like. It can be based on UIButton class and use it's 'selected' state to indicate one from the group. The UIButton has native customisation options in IB, so it is convenient to design XIBs. Also there should be an easy way to group buttons using IB outlet connections:

I have implemented my ideas in this RadioButton class. Works like a charm:

Download the sample project.

onegray
  • 5,105
  • 1
  • 23
  • 29
  • i have downloaded your app. if I copy pest your buttons it works perfectly for me but if I tried to add extra buttons it wont works for me. I have added groupButton, checked, unchecked images , target class to RadioButton but not able to add more buttons that works correctly even though added in your groupButton gesture ? – Swapnil Kotwal Nov 15 '13 at 11:28
25

Try UISegmentedControl. It behaves similarly to radio buttons -- presents an array of choices and lets the user pick 1.

jlstrecker
  • 4,953
  • 3
  • 46
  • 60
20

Just want to sum up, there might be 4 ways.

  • If you don't have much space, add a click event for text or button, then show UIPickerView:

UIPickerView

or open a new table view control with a check mark:

UITableView

  • If there is more space, add a table view directly to your main view:

enter image description here

  • The final solution is using UISegmentedControl:

enter image description here

Hope this helps.

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
8

Try DLRadioButton, works for both Swift and ObjC. You can also use images to indicate selection status or customize your own style.

Check it out at GitHub.

radio button for iOS

**Update: added the option for putting selection indicator on the right side.

**Update: added square button, IBDesignable, improved performance.

**Update: added multiple selection support.

David Liu
  • 16,374
  • 12
  • 37
  • 38
  • Ive just included this in a project im building, thanks! Some notes, the control needs code-behind, it doesnt work using just the designer.. – user230910 May 28 '15 at 07:00
  • @DavidLiu I am trying to run the sample included it is not giving any touch response, can you guide – vishwa.deepak Dec 21 '15 at 15:41
  • 1
    @vishwa.deepak not sure exactly what u mean. I just tested the latest sample, it works perfectly. Let me know if you have further questions. – David Liu Dec 22 '15 at 03:13
  • @DavidLiu Don't know what happened last night It was totally stuck, today when I tried it was working like charm, but I still have one problem, as it is radio button, then atleast one of them should be preselected, or there should be an option for preselection. – vishwa.deepak Dec 22 '15 at 07:12
  • 1
    You can set them programmatically by set 'selected' to YES. – David Liu Dec 22 '15 at 07:30
  • Ok So I should have separate IBoutlets for the buttons even if I have created them in story board. Thanks for the time and reply. – vishwa.deepak Dec 22 '15 at 08:02
  • @vishwa.deepak it all depends on whether you want reference them individually or together. But I recommend have separate iboutles for them. – David Liu Dec 22 '15 at 08:36
  • @DavidLiu Used the later way of creating different iBoutlets for each button,and its working great thanks again – vishwa.deepak Dec 22 '15 at 12:20
8

I know its very late to answer this but hope this may help anyone.

you can create button like radio button using IBOutletCollection. create one IBOutletCollection property in our .h file.

@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *ButtonArray;

connect all button with this IBOutletCollection and make one IBAction method for all three button.

- (IBAction)btnTapped:(id)sender {
       for ( int i=0; i < [self.ButtonArray count]; i++) {
           [[self.ButtonArray objectAtIndex:i] setImage:[UIImage                         
            imageNamed:@"radio-off.png"]                 
            forState:UIControlStateNormal];
       }
     [sender setImage:[UIImage imageNamed:@"radio-on.png"]
      forState:UIControlStateNormal];
}
riddhi
  • 316
  • 2
  • 16
  • 1
    You can move the line where you are setting an image to the sender outside the for loop. So that it won't get executed each time the loop iterates. – ViruMax Apr 08 '17 at 08:41
7

For options screens, especially where there are multiple radio groups, I like to use a grouped table view. Each group is a radio group and each cell a choice within the group. It is trivial to use the accessory view of a cell for a check mark indicating which option you want.

If only UIPickerView could be made just a little smaller or their gradients were a bit better suited to tiling two to a page...

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
5

I've written a controller for handling the logic behind an array of radio buttons. It's open source and on GitHub, check it out!

https://github.com/goosoftware/GSRadioButtonSetController

j0k
  • 22,600
  • 28
  • 79
  • 90
Simon Whitaker
  • 20,506
  • 4
  • 62
  • 79
  • hi Simon! I tried to use GSRadioButtonSetController, it works very nicely.Thanks. But how to deselect the buttons? I tried 'myButton.selected = YES;' but when try to select the button again, it doesn't get selected. any idea? – Frade Mar 22 '13 at 12:58
  • Try `myController.selectedIndex = NSNotFound` – Simon Whitaker Mar 22 '13 at 14:03
  • Thats nice , but can I integrate it with UITableView ? I mean in right corner of cells to have the radio button , and on cell selected to get them selected ? – user2533527 Dec 01 '13 at 00:47
-1

The following simple way to create radio button in your iOS app follow two steps.

Step1- Put this code in your in viewDidLoad or any other desired method

 [_mrRadio setSelected:YES];
        [_mrRadio setTag:1];
        [_msRadio setTag:1];
        [_mrRadio setBackgroundImage:[UIImage imageNamed:@"radiodselect_white.png"] forState:UIControlStateNormal];
        [_mrRadio setBackgroundImage:[UIImage imageNamed:@"radioselect_white.png"] forState:UIControlStateSelected];
        [_mrRadio addTarget:self action:@selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];

        [_msRadio setBackgroundImage:[UIImage imageNamed:@"radiodselect_white.png"] forState:UIControlStateNormal];
        [_msRadio setBackgroundImage:[UIImage imageNamed:@"radioselect_white.png"] forState:UIControlStateSelected];
        [_msRadio addTarget:self action:@selector(radioButtonSelected:) forControlEvents:UIControlEventTouchUpInside];

Step2- Put following IBAction method in your class

-(void)radioButtonSelected:(id)sender
{
    switch ([sender tag ]) {
        case 1:
            if ([_mrRadio isSelected]==YES) {
              //  [_mrRadio setSelected:NO];
               // [_msRadio setSelected:YES];
               genderType = @"1";
            }
            else
            {
                [_mrRadio setSelected:YES];
                [_msRadio setSelected:NO];
                genderType = @"1";
            }
            break;
        case 2:
            if ([_msRadio isSelected]==YES) {
               // [_msRadio setSelected:NO];
               // [_mrRadio setSelected:YES];
                genderType = @"2";
            }
            else
            {
                [_msRadio setSelected:YES];
                [_mrRadio setSelected:NO];
                 genderType = @"2";
            }
            break;
        default:
            break;
    }
}