在iOS中如果要使用類似安卓中的RadioButton,就會發現比較麻煩,因為在iOS中沒有自帶的RadioButton的官方控件,只能自己實現或者使用第三方庫。
先上一張圖:
就拿上圖來舉例,上圖用到了兩個RadioButton了,在每一類里只允許有一個選項,如果用戶點擊了新的選項,那么以前的選項就失效了,并且高亮選擇的是最新的選項。
由于功能比較簡單,我選擇了自己實現RadioButton,雖然cocoapods上有很多已經造好了的輪子,但是我認為像這種很簡單的控件能自己寫的盡量還是不要用第三放的為好,閑話不多說了。
RadioButton的實現思路是做一個遍歷,每當同一組的RadioButton的狀態發生改變之后對所有的button進行一個遍歷,把最新點擊的button的selected屬性更新為YES,把其他的button的selected屬性更新為NO。
self.timeBtnArr = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 1; i<=4; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = baseTag + i;
button.titleLabel.font = [UIFont systemFontOfSize:9];
[button setTitleColor:[UIColor colorFromHexRGB:@"595959"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorFromHexRGB:@"ed7e2d"] forState:UIControlStateSelected];
button.layer.cornerRadius = 3;
button.layer.borderWidth = 0.5;
button.layer.borderColor = [UIColor colorFromHexRGB:@"595959"].CGColor;
button.layer.masksToBounds = YES;
[button addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
switch (i) {
case 1:
[button setTitle:@"不限" forState:UIControlStateNormal];
button.additionInfo = @"";
break;
case 2:
[button setTitle:@"十五天內" forState:UIControlStateNormal];
button.additionInfo = @"15";
break;
case 3:
[button setTitle:@"十天內" forState:UIControlStateNormal];
button.additionInfo = @"10";
break;
case 4:
[button setTitle:@"五天內" forState:UIControlStateNormal];
button.additionInfo = @"5";
break;
default:
break;
}
[self.timeBtnArr addObject:button];
}
用for循環添加了一個具有四個button的RadioButton,以后在_timeBtnArr中對RadioButton的狀態進行操作。
看一下觸發按鈕狀態的代碼:
- (void)btnDidClicked:(UIButton *) btn{
for (UIButton *button in self.timeBtnArr) {
if (button.tag == btn.tag) {
button.layer.borderColor = [UIColor colorFromHexRGB:@"ed7e2d"].CGColor;
button.selected = YES;
}
else{
button.layer.borderColor = [UIColor colorFromHexRGB:@"595959"].CGColor;
button.selected = NO;
}
}
}
每當RadioButton的狀態改變時,都會從新更新一下RadioButton中button的狀態。
但是,上面這么做跟業務的要求還是有所偏差,因為需要將RadioButton的選擇狀態保存下來。雖然可以直接將選中狀態的title保存下來,但是這樣顯然會增加數據存儲量,而且也不夠優雅!所以,我選擇將每個button增加一個附加屬性,用來設置button所代表的含義。于是,需要為UIButton類增加一個類別,來添加一個additionInfo的屬性。
//
// UIButton+HFExtend.h
// xStore
//
// Created by apple on 16/8/10.
// Copyright ? 2016年 apple. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIButton (HFExtend)
@property(copy, nonatomic) NSString *additionInfo;
@end
//
// UIButton+HFExtend.m
// xStore
//
// Created by apple on 16/8/10.
// Copyright ? 2016年 apple. All rights reserved.
//
#import "UIButton+HFExtend.h"
#import "objc/runtime.h"
static void *additionInfoKey = &additionInfoKey;
@implementation UIButton (HFExtend)
- (void)setAdditionInfo:(NSString *)additionInfo{
objc_setAssociatedObject(self, &additionInfoKey, additionInfo, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *) additionInfo{
return objc_getAssociatedObject(self, &additionInfoKey);
}
@end
這樣以后就能通過Button的additionInfo的屬性來存儲附加信息了。以后保存RadioButton的狀態直接保存選擇button的additionInfo屬性即可。