如何制作多个单选按钮,但在第一个默认设置的情况下始终只选择一个单选按钮?

How to make multiple radioButtons but only one is always selected where the first is default?

我从事一个 Objective-C 项目并尝试构建多个单选按钮。很快我想制作多个单选按钮,但只有一个总是被选中,第一个是默认的。

我开始尝试使用 2 个按钮,但无法完全达到我想要的效果。我希望有好的方法可以做到这一点。我也同意扩展。

您可以通过将 UI 个元素放在 UIView 中来对它们进行分组,并使用 KVO 观察器来利用额外的代码。

@interface ViewController ()
@property (nonatomic) UIView *group;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createGroupWithRadios:3 andSelectIndex:0];
}

//a context to make KVO much easier to catch (and less string compare'y)
static void * kRadioGroupContext = &kRadioGroupContext;

-(void)createGroupWithRadios:(int)amount andSelectIndex:(int)idx {
    CGFloat h = 30.0;
    _group = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, h*amount)];
    for (int i=0; i<amount; i++) {
        UIButton *radio = [UIButton buttonWithType:(UIButtonTypeSystem)];
        radio.tag = i;
        radio.selected = i==idx;
        { //style stuff
            radio.frame = CGRectMake(0, h*i, 100, h);
            [radio setTitle:@"off" forState:UIControlStateNormal];
            [radio setTitle:@"on" forState:UIControlStateSelected];
            [radio setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
            [radio setTitleColor:UIColor.redColor forState:UIControlStateSelected];
            //[radio setImage:[UIImage imageNamed:@"active"] forState:UIControlStateSelected];
            //[radio setImage:[UIImage imageNamed:@"inactive"] forState:UIControlStateNormal];
        }
        [radio addTarget:self action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [_group addSubview:radio];
    }
    [self.view addSubview:_group];

    //you can use _group.tag to keep which one is selected and observe it
    _group.tag = idx;
    [_group addObserver:self forKeyPath:@"tag" options:(NSKeyValueObservingOptionNew) context:kRadioGroupContext];
}

-(void)radioButtonPressed:(UIButton*)sender {
    _group.tag = sender.tag;
    for (int i=0; i<_group.subviews.count; i++) {
        UIButton *radio = _group.subviews[i];
        radio.selected = radio.tag==sender.tag;
    } 
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if (context == kRadioGroupContext) {
        // do something when a new radio is selected
        NSLog(@"selected Radio %lu", _group.tag);
    }
}

如前所述UIControl 是 UIView 的子类,这意味着您也可以使用 UIControl 将您的 UIButton 组合在一起 .优点是在做出新选择时添加目标很容易捕捉。缺点是你必须多考虑一下 styling/layout 因为它在代码中是固定的并且改变需要更多的关注。下面的示例使用 NSStrings 的 NSArray 作为 UIControl 按钮内的标题传播。因此 NSArray 计数定义了在框架高度中生成了多少 Button。用这个应该很容易建立多项选择表格。

//ButtonSelectGroup.h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ButtonSelectGroup : UIControl
-(instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray<NSString*>*)titles preValue:(NSInteger)idx;
@property (nonatomic) NSInteger value;
-(void)setTitles:(NSArray<NSString *> * _Nonnull)titles;
@end

NS_ASSUME_NONNULL_END

//ButtonSelectGroup.m
#import "ButtonSelectGroup.h"

@interface ButtonSelectGroup () {
    NSInteger oldValue;
}
@end

@implementation ButtonSelectGroup
-(instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray<NSString*>*)titles preValue:(NSInteger)idx {
    if (!(self=[super initWithFrame:frame])) return nil;
    [self createGroupWithPreSelectIndex:idx andTitles:titles];
    return self;
}
-(void)createGroupWithPreSelectIndex:(NSInteger)idx andTitles:(NSArray<NSString*>*)titles {
    NSInteger amount = titles.count;
    CGFloat h = self.frame.size.height / amount;
    for (NSInteger i=0; i<amount; i++) {
        UIButton *radio = [UIButton buttonWithType:(UIButtonTypeSystem)];
        radio.tag = i;
        radio.selected = i==idx;
        { //style stuff
            radio.frame = CGRectMake(0, h*i, 100, h);
            [radio setTitle:titles[i] forState:UIControlStateNormal];
            [radio setTitle:titles[i] forState:UIControlStateSelected];
        }
        [radio addTarget:self action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:radio];
    }
    self.value = idx;
}
-(void)radioButtonPressed:(UIButton*)sender {
    oldValue = _value;
    for (NSInteger i=0; i<self.subviews.count; i++) {
        UIButton *radio = self.subviews[i];
        radio.selected = radio.tag==sender.tag;
    }
    _value = sender.tag;
    if (_value != oldValue) [self sendActionsForControlEvents:(UIControlEventValueChanged)];
}
-(void)setTitles:(NSArray<NSString *> *)titles {
    for (NSInteger i=0; i<self.subviews.count && i<titles.count; i++) {
        UIButton *radio = self.subviews[i];
        [radio setTitle:titles[i] forState:UIControlStateNormal];
        [radio setTitle:titles[i] forState:UIControlStateSelected];
    }
}
@end

然后从您的 ViewController.

按以下方式使用 #import "ButtonSelectGroup.h"
-(void)viewDidLoad {
    [super viewDidLoad];
    ButtonSelectGroup *group = [[ButtonSelectGroup alloc] initWithFrame:CGRectMake(0, 0, 100, 300) andTitles:@[@"even",@"donald",@"duck",@"would",@"concede"] preValue:0];
    [self.view addSubview:group];
    [group addTarget:self action:@selector(singularChoice:) forControlEvents:(UIControlEventValueChanged)];
    // and changing all titles again if you wish
    //group.titles = @[@"but",@"not",@"orangeman"];
}
-(void)singularChoice:(id)sender {
    ButtonSelectGroup* group = (ButtonSelectGroup*)sender;
    UIButton *btn = group.subviews[group.value];
    NSLog(@"groups selection = %lu %@",group.value, btn.titleLabel.text);
}