华山论剑之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


希望这个小工具能对大家有所帮助.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,853评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,930评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 孤悬天穹已万古,人间红尘又望断。晓得寂寥生寒处,华景梦幻不胜高。天河相隔无穷远,冷星对照影自怜。嫦娥仙踪迹难觅,圆...
    KenChoi阅读 262评论 0 0
  • 2017年3月20日星期一 阴转晴 今天周一了,我昨天没写日记,真的是忘记,但不得不说,我真的很~ 说实话,我好像...
    織帯阅读 128评论 0 0