码迷,mamicode.com
首页 > 移动开发 > 详细

iOS8中UIAlertController的使用

时间:2014-09-25 12:48:38      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   io   os   使用   sp   cti   on   c   log   

iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,将它们集合成了自己的style;

1------------UIAlertControllerStyleAlert-----原有的UIAlertView

 UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

    

    //preferredStyle--只读

    //打印选择的AlertController的类型 actionSheet--0 alertView--1

    NSLog(@"%ld",alertC.preferredStyle);

    //获取alertC的标题和信息

    NSLog(@"%@",alertC.title);

    NSLog(@"%@",alertC.message);

    //更改标题

    alertC.title = @"title change";

    

    //直接给alertC添加事件执行按钮actionTitle(只能添加一个)

    /*

     [alertC addAction:[UIAlertAction actionWithTitle:@"actionTitle" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

     //点击actionTitle时的回调方法

     }]];

     */

    

    //添加多个按钮

    //没有任何代理的情况下,在我们点击alertView界面中的按钮时,就不需要用buttonIndex来区分我们点击的是哪个按钮,同时一个界面上多个alertView时,也不需要用tag值来区分你到底点击的是哪一个alertView上的按钮了

    //这样便于我们的逻辑思维,但是代码量并没有减少

    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

                                                     style:UIAlertActionStyleCancel

                                                   handler:^(UIAlertAction *action) {

                                                       //cancle对应执行的代码

                                                       NSLog(@"cancle action");

                                                   }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

                                                      style:UIAlertActionStyleDefault

                                                    handler:^(UIAlertAction *action) {

                                                        //sure对应执行的代码

                                                        NSLog(@"sure action");

                                                    }];

    

    [alertC addAction:action];

    [alertC addAction:action1];

    

    //alertView的输入框 sheet类型中没有 但是sheet类型的UIAlertController可以添加textField 当用户要显示sheet时程序会崩

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder = @"textFiled";

    }];

    

    //在这里不管是alertView还是actionSheet都是一个独立的Cotroller,所以这里需要通过presentViewController来展现我们的alertView或者actionSheet

    [self presentViewController:alertC animated:YES completion:nil];

 

2---------------UIAlertControllerStyleActionSheet----原有的UIActionSheet

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

                                                     style:UIAlertActionStyleCancel

                                                   handler:^(UIAlertAction *action) {

                                                       NSLog(@"cancle action");

                                                   }];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

                                                      style:UIAlertActionStyleDefault

                                                    handler:^(UIAlertAction *action) {

                                                        NSLog(@"sure action");

                                                    }];

    

    [alertC addAction:action];

    [alertC addAction:action1];

    [self presentViewController:alertC animated:YES completion:nil];

iOS8中UIAlertController的使用

标签:style   io   os   使用   sp   cti   on   c   log   

原文地址:http://www.cnblogs.com/NINIiOS/p/3992247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!