【iOS开发】UIAlertController

作者: 0o冻僵的企鹅o0 | 来源:发表于2016-05-26 16:26 被阅读801次

    随着iOS 10即将到来,有些刚开始的项目便不再支持iOS 7,而是从iOS 8开始。而UIAlertController正好是iOS 8推出用来替代UIAlertView和UIActionSheet的。我们来看看UIAlertController是怎样使用的。

    下面代码展示了一个简单的 alert controller:

        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                                       message:@"This is an alert."
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  UITextField *login = alert.textFields.firstObject;
                                                              }];
        UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {}];
        [alert addAction:cancelAction];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    

    外观如下图所示:

    1.png

    从代码来看,UIAlertController的回调跟UIAlertView和UIActionSheet不同,UIAlertView和UIActionSheet是采用代理,而UIAlertController用的是block。block确实要方便很多。
    创建UIAlertController的方法很简单:

    + (instancetype)alertControllerWithTitle:(NSString *)title
                                     message:(NSString *)message
                              preferredStyle:(UIAlertControllerStyle)preferredStyle
    

    title:标题,告诉用户你为什么要展示这个警报。
    message:更加详细的描述。
    preferredStyle:警报样式,UIAlertControllerStyleActionSheet和UIAlertControllerStyleAlert,即以前使用的alert view 和action sheet。

    UIAlertAction,警报动作,创建方法:

    + (instancetype)actionWithTitle:(NSString *)title
                              style:(UIAlertActionStyle)style
                            handler:(void (^)(UIAlertAction *action))handler
    

    title:标题;
    style:样式,有以下三种:

    typedef enum UIAlertActionStyle: NSInteger {
       UIAlertActionStyleDefault = 0,
       UIAlertActionStyleCancel,
       UIAlertActionStyleDestructive 
    } UIAlertActionStyle;
    

    UIAlertActionStyleDefault为默认样式,比如上图中的"OK",
    UIAlertActionStyleCancel为取消样式,如上图中的”Cancel“,个人感觉就是字体更粗了一些,
    UIAlertActionStyleDestructive,为破坏样式,一般表示强烈警告,比如删除,这个在iOS 8之前是没有的(如图2所示)。
    handler:用户触摸alert action所触发的事件回调。

    此外,alert action还有个很有用的属性:enable。可以设置它的可用性,比如alert view文本没有输入时不让点击OK按钮。

    2.png

    文本输入

    另外在使用alert的时候,我们经常会用到文本输入,而UIAlertController已经为我们提供了非常方便的方法:

    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"姓名";
            [textField addTarget:self
                          action:@selector(alertTextFieldDidChange:)
                forControlEvents:UIControlEventEditingChanged];
        }];
    
    - (void)alertTextFieldDidChange:(UITextField *)sender
    {
        UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
        if (alertController)
        {
            UITextField *login = alertController.textFields.firstObject;
            UIAlertAction *okAction = alertController.actions.lastObject;
            okAction.enabled = login.text.length > 0;
        }
    }
    

    最棒的是这里的文本输入框是可以无限制的添加的!
    不过需要注意的是,文本输入框只有alert view才能使用,action sheet是没有的。
    效果如下图所示:


    3.png

    Action Sheet

    只需要将preferredStyle修改为UIAlertControllerStyleActionSheet即可,

        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                                       message:@"This is an alert."
                                                                preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction* deleteAction = ...;
        UIAlertAction* defaultAction = ...;
        UIAlertAction* cancelAction = ...;
    
        [alert addAction:deleteAction];
        [alert addAction:cancelAction];
        [alert addAction:defaultAction];
        
        [self presentViewController:alert animated:YES completion:nil];
    

    效果如下图所示:

    4.png

    从代码可以看到,虽然cancel按钮是在中间加进去的,但是却一直会在底部,而其它按钮则会按照加入的先后顺序从上往下排布。另外在iOS的人机交互指南中,建议我们把destructive action放在第一。

    End.Have fun~

    相关文章

      网友评论

        本文标题:【iOS开发】UIAlertController

        本文链接:https://www.haomeiwen.com/subject/nwhxdttx.html