Best radio-button implementation for IOS

后端 未结 8 573
心在旅途
心在旅途 2020-11-29 20: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 selectio

相关标签:
8条回答
  • 2020-11-29 21:28

    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.

    0 讨论(0)
  • 2020-11-29 21:30

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

    0 讨论(0)
  • 2020-11-29 21:34

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 21:38

    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.

    0 讨论(0)
  • 2020-11-29 21:40

    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

    0 讨论(0)
  • 2020-11-29 21:43

    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.

    0 讨论(0)
提交回复
热议问题