<pre>
GitHub源碼:RadioButton
star:200+
??????
以下內(nèi)容來源于官方源碼个束、 README 文檔泞辐、測(cè)試 Demo或個(gè)人使用總結(jié) 涛浙!
Radio Button for iOS——iOS單選按鈕
README.md文檔描述
繼承自 UIButton 的一個(gè)非常簡(jiǎn)單的類,擴(kuò)展了標(biāo)準(zhǔn)的 UIButton 的功能团驱。
可以為每個(gè)按鈕配置默認(rèn)和選中狀態(tài)沮尿。
[圖片上傳失敗...(image-f3df77-1510566148035)]
非常容易使用
它不需要任何 central manager丛塌。 只需在Interface Builder中直接鏈接按鈕:
[圖片上傳失敗...(image-9f09be-1510566148035)]
或者,使用單行代碼對(duì)按鈕進(jìn)行分組:
radio1.groupButtons = @[radio1, radio2, radio3];
設(shè)置任意按鈕為選中狀態(tài)蛹找,同一組中的所有其他按鈕將自動(dòng)取消選中狀態(tài):
radio2.selected = YES; // radio1 and radio3 become deselected
組中的任何一個(gè)按鈕都知道哪一個(gè)被選中了:
RadioButton* r1 = radio1.selectedButton;
RadioButton* r2 = radio2.selectedButton;
RadioButton* r3 = radio3.selectedButton;
NSAssert (r1==r2 && r2==r3, @"Must be equal");
另一個(gè)通過標(biāo)簽選擇按鈕的好方法:
[radio1 setSelectedWithTag:kTagRadio3];
資源
sample.zip - Sample app
radio-res-ios.zip - Radio Button images used in the sample
許可
RadioButton 基于 MIT許可協(xié)議,—你可以基于你的意愿fork哨坪、覆蓋和使用
實(shí)踐使用
參照官方Demo庸疾,學(xué)習(xí)使用 RadioButton。
首先需要導(dǎo)入RadioButton.h 和 RadioButton.m 文件当编;
其次還需要兩張圖片用于顯示按鈕選中(check)和未選中(unchecked)狀態(tài)届慈。
1. 一組中有三個(gè)單選按鈕
[圖片上傳失敗...(image-c3633f-1510566148035)]
- 首先在Interface Builder界面中拖入3個(gè) UIButton 控件、1個(gè) UILabel 控件忿偷;
[圖片上傳失敗...(image-6bee78-1510566148035)]
- 選中第一個(gè) UIButton 控件金顿,修改 Identity Inspector 工具欄中的class屬性為:Radio Button;
[圖片上傳失敗...(image-89eaa1-1510566148035)]
- 還是在選中第一個(gè) UIButton 控件下鲤桥,切換到 Attributes Inspector 工具欄中修改相關(guān)屬性如下:
其中你需要分開設(shè)置 Button按鈕在不同狀態(tài)下顯示的圖片(藍(lán)框所示):
Default 默認(rèn)狀態(tài)下設(shè)置的 Image 是:unchecked.png;
Selected選中狀態(tài)下設(shè)置的 Image 是:checked.png;
另外勾選設(shè)置第一個(gè) Button 按鈕的初始化狀態(tài)是默認(rèn)選中的(綠框所示)揍拆。
[圖片上傳失敗...(image-b21e12-1510566148035)]
[圖片上傳失敗...(image-c94b07-1510566148035)]
-
剩下兩個(gè) Button 按鈕也相同設(shè)置,需要區(qū)分設(shè)置的是Title標(biāo)題茶凳、另外兩個(gè)按鈕都不用勾選默認(rèn)選中狀態(tài)嫂拴,設(shè)置完成后應(yīng)該是這樣的:
[圖片上傳失敗...(image-2385d5-1510566148034)]
設(shè)置群組,在 Radio Button1 上鼠標(biāo)右擊贮喧,找到 groupButtons 屬性筒狠,長(zhǎng)按?連接到 Radio Button2 ,再如法炮制箱沦,將 Radio Button2 的 groupButtons 連接到 Radio Button3辩恼;
[圖片上傳失敗...(image-285869-1510566148035)]
修改 UILabel 控件的默認(rèn)Text屬性顯示為:Radio Button1,因?yàn)镽adio Button1是默認(rèn)選中的谓形,所以默認(rèn)文本也應(yīng)該顯示這個(gè):
-
關(guān)聯(lián)到代碼:
- 在viewController.h 文件中先聲明Radio Button類;
@class RadioButton;
- 關(guān)聯(lián)以上幾個(gè)控件屬性灶伊;
@property (strong, nonatomic) IBOutlet RadioButton *radioButton1; @property (strong, nonatomic) IBOutlet RadioButton *radioButton2; @property (strong, nonatomic) IBOutlet RadioButton *radioButton3; @property (strong, nonatomic) IBOutlet UILabel *statusLabel;
- 在 viewController.m 文件中導(dǎo)入
import "RadioButton.h"
-
將3個(gè)Radio Button按鈕同時(shí)關(guān)聯(lián)到一個(gè)方法上:
- (IBAction)onRadioBtn:(RadioButton *)sender { _statusLabel.text = [NSString stringWithFormat:@"%@",sender.titleLabel.text]; }
構(gòu)建運(yùn)行。
2.一組中有兩個(gè)按鈕
[圖片上傳失敗...(image-9a03e8-1510566148035)]
該例與1的類似寒跳,就不詳細(xì)說了谁帕,只是【切換】是一個(gè)標(biāo)準(zhǔn)的 UIButton 按鈕,該按鈕關(guān)聯(lián)的方法如下:
- (IBAction)radioBtnInvert:(UIButton *)sender {
_man.selected = !_man.selected;
}
3.代碼創(chuàng)建方法
[圖片上傳失敗...(image-c217ce-1510566148035)]
點(diǎn)擊按鈕創(chuàng)建一個(gè)包含三個(gè)按鈕的群組
代碼方法:
- (IBAction)createMoreBtn:(UIButton *)sender {
[sender setHidden:YES];
NSMutableArray* buttonsArray = [NSMutableArray arrayWithCapacity:3];
CGRect btnRect = CGRectMake(25, 320, 100, 30);
for (NSString* optionTitle in @[@"Red", @"Green", @"Blue"]) {
RadioButton* btn = [[RadioButton alloc] initWithFrame:btnRect];
[btn addTarget:self action:@selector(onRadioButtonValueChanged:) forControlEvents:UIControlEventValueChanged];
btnRect.origin.y += 40;
[btn setTitle:optionTitle forState:UIControlStateNormal];
[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
[self.view addSubview:btn];
[buttonsArray addObject:btn];
}
[buttonsArray[0] setGroupButtons:buttonsArray]; // 把按鈕放進(jìn)群組中
[buttonsArray[0] setSelected:YES]; // 初始化第一個(gè)按鈕為選中狀態(tài)
}
其他
作者:獨(dú)木舟的木
鏈接:http://www.reibang.com/p/b349428b40ab
來源:簡(jiǎn)書
著作權(quán)歸作者所有冯袍。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán)匈挖,非商業(yè)轉(zhuǎn)載請(qǐng)注明出處碾牌。</pre>