生命縱容讓自己變得平凡,卻不能讓心失去對(duì)希望的追求.
前兩天在工作中遇到一個(gè)對(duì)過(guò)的選項(xiàng)進(jìn)行選擇,一開始的時(shí)候,我是一個(gè)選項(xiàng)一個(gè)選項(xiàng)的添加,發(fā)現(xiàn)代碼的重復(fù)率實(shí)在是太高了,所以,我就封裝了一個(gè)方法,這里我對(duì)這個(gè)方法的參數(shù)進(jìn)行一下講解
-
titleString:本參數(shù)是彈窗的標(biāo)題
-
array:本參數(shù)是一個(gè)數(shù)組對(duì)象,里面存儲(chǔ)的是我們所需要的所有的選項(xiàng),
-
label:這個(gè)label對(duì)象是我們需要顯示我們選擇的是那個(gè)一個(gè)選項(xiàng),這里我們可以對(duì)label進(jìn)行手勢(shì)的添加,來(lái)實(shí)現(xiàn)的這個(gè)事件的點(diǎn)擊
-(void)addAlertViewControllerWithTitle:(NSString *)titleString menssage:(NSArray *)array showlable:(UILabel *)label{
UIAlertController * alertCtr2 = [UIAlertController alertControllerWithTitle:titleString message:nil preferredStyle:UIAlertControllerStyleAlert];
for (int i = 0; i< array.count; i++) {
NSString *string = array[i];
// Create the actions.
UIAlertAction *ButtonAction = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
label.text = string;
}];
[alertCtr2 addAction:ButtonAction];
}
[self presentViewController:alertCtr2 animated:YES completion:nil];
}
多彈窗選擇菜單使用范例
這里我還是在ViewController中進(jìn)行范例的演示.給看管吧代碼復(fù)制到自己Demo種查看就好
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
_label.text = @"點(diǎn)擊選擇選項(xiàng)";
//lable添加一個(gè)tap手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(presentChooseAlertView)];
_label.userInteractionEnabled = YES;
[_label addGestureRecognizer:tap];
[self.view addSubview:_label];
}
-(void)presentChooseAlertView{
[self addAlertViewControllerWithTitle:@"成為一個(gè)程序猿的必備條件" menssage:@[@"靈活的頭腦",@"傲嬌的內(nèi)心",@"讀過(guò)<<活著>>"] showlable:_label];
}
-(void)addAlertViewControllerWithTitle:(NSString *)titleString menssage:(NSArray *)array showlable:(UILabel *)label{
UIAlertController * alertCtr2 = [UIAlertController alertControllerWithTitle:titleString message:nil preferredStyle:UIAlertControllerStyleAlert];
for (int i = 0; i< array.count; i++) {
NSString *string = array[i];
// Create the actions.
UIAlertAction *ButtonAction = [UIAlertAction actionWithTitle:string style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
label.text = string;
}];
[alertCtr2 addAction:ButtonAction];
}
[self presentViewController:alertCtr2 animated:YES completion:nil];
}
@end
希望這個(gè)小工具能對(duì)大家有所幫助.