华山论剑之iOS自定义选择菜单弹窗

生命纵容让自己变得平凡,却不能让心失去对希望的追求.

前两天在工作中遇到一个对过的选项进行选择,一开始的时候,我是一个选项一个选项的添加,发现代码的重复率实在是太高了,所以,我就封装了一个方法,这里我对这个方法的参数进行一下讲解


  • titleString:本参数是弹窗的标题
  • array:本参数是一个数组对象,里面存储的是我们所需要的所有的选项,
  • label:这个label对象是我们需要显示我们选择的是那个一个选项,这里我们可以对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];
    
}



多弹窗选择菜单使用范例


这里我还是在ViewController中进行范例的演示.给看管吧代码复制到自己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 = @"点击选择选项";
    
    //lable添加一个tap手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(presentChooseAlertView)];
    
    _label.userInteractionEnabled = YES;
    
    [_label addGestureRecognizer:tap];
    
    
    [self.view addSubview:_label];
    

}


-(void)presentChooseAlertView{


    [self addAlertViewControllerWithTitle:@"成为一个程序猿的必备条件" menssage:@[@"灵活的头脑",@"傲娇的内心",@"读过<<活着>>"] 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


希望这个小工具能对大家有所帮助.

你可能感兴趣的:(华山论剑之iOS自定义选择菜单弹窗)