UIAlertController

在iOS8以后,UIAlertView和UIActionSheet的使用统一到UIAlertController,下面就来一起学习记录下UIAlertController的基本使用

  • UIAlertView创建直接上代码
  • UIAlertController  *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"你好" preferredStyle:UIAlertControllerStyleAlert];

    其中的第一个参数是UIAlertController的主标题,message为副标题,preferredStyle为样式,也就是前面说的UIAlertView和UIActionSheet两种样式,是个枚举类型,具体可以按住option+鼠标左键点击进去查看,如下

    typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
        UIAlertControllerStyleActionSheet = 0,
        UIAlertControllerStyleAlert
    } NS_ENUM_AVAILABLE_IOS(8_0);

    这是在iOS8以后才使用的,选第一个UIAlertControllerStyleActionSheet就是创建的UIAlertView,选第二个就是创建的UIActionSheet

  • 创建完成后显示的效果是这样的,通过下面这句代码显示
    [self presentViewController:alertController animated:YES completion:nil];

     

 

  • 下面为UIAlertController添加点击按钮,UIAlertController添加按钮的方式与UIAlertView的不一样,这里是通过UIAlertAction这个类来添加的,没添加一个这样的实例就创建一个按钮,UIAlertAction后边有一个句柄handler,也就是一个Block。下面为他添加两个按钮"取消"和"确定",并为他们添加点击事件(不像UIAlertView和UIActionSheet一样要设置代理,实现代理)。
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault  handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
    UIAlertAction
    *oklAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了确定"); }];
    [alertController addAction:cancelAction]; [alertController addAction:oklAction];

     其中style是按钮的样式,系统提供三种,自己可以点击进去选择,个人认为就是按钮的字体改变了。显示效果如下

  • 点击"取消"和"确定"就会通过handler进行回调,处理我们相应的逻辑。其中按钮的排列次序是由添加的顺序决定的。
  • 为UIAlertController添加文本框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"密码";
            textField.secureTextEntry = YES;
        }];

     这里可以直接添加textField,handler里面能获取textField,我们可以对他进行一些相应的设置。

  • 显示效果如下

问题来了,那我当我们想要获取用户输入的信息呢,也就是textField里面的内容。这个苹果当然考虑到了,因为在UIAlertController内部有许多的数组,话不多说我们可以进入里面查看。

 

  • 想要什么信息,自己去这里面的数组找就好了。 
  • UIActionSheet的创建
    UIAlertController  *alertController = [UIAlertController alertControllerWithTitle:@"最爱江颖婷" message:@"爱我么?" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault  handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
    UIAlertAction *oklAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault  handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了确定");
        }];
    UIAlertAction
    *hilAction = [UIAlertAction actionWithTitle:@"江颖婷" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了确定"); }]; [alertController addAction:hilAction]; [alertController addAction:oklAction]; [alertController addAction:cancelAction];

     显示效果如下:
    为了纪念某某某,就把她打上去了,我怕以后会忘记她。

 

  • 自此,UIAlertController的基本功能介绍完了,也是对自己学习的一个记录。

 

posted @ 2016-07-27 12:01  降妖除魔来深圳  阅读(241)  评论(0编辑  收藏  举报