IOS 的最佳单选按钮实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4847296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:43:29  来源:igfitidea点击:

Best radio-button implementation for IOS

iphonecocoa-touchiosuibuttonradio-button

提问by Jonas Arcangel

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

我想问一下是否有关于如何在 iPhone 应用程序上实现单选按钮选项的示例。

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

我发现 Picker View 对于一个简单的选择功能来说非常大。

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.

我不确定 Apple 是否故意排除单选按钮,以及从可用性/用户体验的角度简单地使用 Picker View 是否更好。

回答by onegray

I have some thoughts on how the best radio button implementation should look like. It can be based on UIButtonclass and use it's 'selected' state to indicate one from the group. The UIButtonhas 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:

我对最好的单选按钮实现应该是什么样子有一些想法。它可以基于UIButton类并使用它的“已选择”状态来指示组中的一个。在UIButton拥有IB本地定制选项,所以很方便设计XIBs。还应该有一种使用 IB 插座连接对按钮进行分组的简单方法:

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

我已经在这个RadioButton类中实现了我的想法。奇迹般有效:

Download the sample project.

下载示例项目

回答by jlstrecker

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

试试UISegmentedControl。它的行为类似于单选按钮——呈现一系列选项并让用户选择 1。

回答by thanhbinh84

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

只想总结一下,可能有4种方法。

  • If you don't have much space, add a click event for text or button, then show UIPickerView:
  • 如果你没有太多空间,为文本或按钮添加一个点击事件,然后显示 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:
  • 最终的解决方案是使用 UISegmentedControl:

enter image description here

在此处输入图片说明

Hope this helps.

希望这可以帮助。

回答by riddhi

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.

您可以使用IBOutletCollection. 在我们的 .h 文件中创建一个 IBOutletCollection 属性。

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

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

将所有按钮与此 IBOutletCollection 连接起来,并为所有三个按钮创建一个 IBAction 方法。

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

回答by David Liu

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

尝试DLRadioButton,适用于SwiftObjC。您还可以使用图像来指示选择状态或自定义您自己的样式。

Check it out at GitHub.

GitHub 上查看。

radio button for iOS

iOS 单选按钮

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

**更新:添加了在右侧放置选择指示器的选项。

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

**更新:添加方形按钮IBDesignable,提高性能。

**Update: added multiple selection support.

**更新:添加了多选支持。

回答by Adam Eberbach

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...

如果 UIPickerView 可以做得更小一点,或者它们的渐变更适合将两个平铺到一个页面......

回答by Simon Whitaker

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!

我已经编写了一个控制器来处理一组单选按钮背后的逻辑。它是开源的,在 GitHub 上,看看吧!

https://github.com/goosoftware/GSRadioButtonSetController

https://github.com/goosoftware/GSRadioButtonSetController

回答by Mohammad Kamran Usmani

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

下面简单的方法在您的iOS应用创建单选按钮遵循两个步骤。

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

Step1- 将此代码放入您的 viewDidLoad 或任何其他所需的方法中

 [_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

Step2- 将以下 IBAction 方法放入您的类中

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