iOS 使用IBOutletCollection实现RadioButton功能

功能描述:

  用RadioButton和Checkbox做一个功能选择列表,让客户选择功能项,点击保存后,屏蔽RadioButton和Checkbox选项框,并保持选择☑️状态。

  

实现关键代码:

1. RadioButton的实现:

  参考“iOS单选框RadioButton的实现” http://ios.jobbole.com/84465/

  使用IBOutletCollection将几个Button link起来,IBOutletCollection“这个关键字,可以将界面上一组相同的控件连接到同一个数组中通常情况下,我们使用一个IBOutletCollection属性时,属性必须是strong的,且类型是NSArray。具体想了解更多IBOutletCollection,可以参考:IBOutletCollection的使用方法 – CocoaChina_让移动开发更简单。这里用IBOutletCollection主要还是为了方便能够在IB中方便操作,其实用NSArray实现也是一样的。”

 

2. 点击“保存”按钮后,Disable“功能选项”中的RadioButton的选择功能。

  主要完成的功能是

  a. Disable所有RadioButton为不可选择

  b. 保持RadioButton的选择状态及Image属性

  需要:“通过设置正常状态下的按钮的skin,禁用图片的状态”:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:@"normalState"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"normalState"] forState:UIControlStateDisabled];

  实际实现(结合RadioButton的实现)

- (IBAction)saveInfo:(UIButton *)sender {
    
    // Disable button function
    [sender setEnabled:NO];
    sender.backgroundColor = [UIColor darkGrayColor];
    [sender.layer setBorderColor:[UIColor darkGrayColor].CGColor];
    
    // Disable radio button
    if ([_radioBtn.groupButtons count])
    {
        for (UIButton *btns in _radioBtn.groupButtons)
        {
            [btns setEnabled:NO];
//            NSLog(@"btns = %@", btns.titleLabel.text);
//            NSLog(@"state = %lu", (unsigned long)btns.state);
            
            UIImage *btnImage;
            if (btns.state & UIControlStateSelected)
                btnImage = [btns imageForState: UIControlStateSelected];
            else
                btnImage = [btns imageForState: UIControlStateNormal];
            [btns setImage:btnImage forState: btns.state];
        }
    }
    
    // Disable checkbox button
    
}
View Code

 

posted on 2016-06-30 15:15  Rosa.Bai  阅读(359)  评论(0)    收藏  举报