美文网首页iOS 开发
UIAlertController 的使用学习

UIAlertController 的使用学习

作者: Jun_Sleepy | 来源:发表于2016-07-09 11:37 被阅读105次

    最近刚入手学习IOS,第一个教程用的是《让不懂编程的人爱上iPhone开发(2013 iOS7版),其中当选择在IOS 9.x deploy的时候,教程中UIAlertView已经不再适用,提示要用UIAlertController替代,所以学习了一下,特在此记录。

    这次学习主要参考了:
    Ammar 的 简书文章
    一篇博客:博客

    #import"ViewController.h"
    @interfaceViewController()
    - (IBAction)showAlert:(id)sender;
    @end
    - (IBAction)showAlert:(id)sender {
    //下面内容的代码是放置在这里的
    }
    

    UIAlertControllerStyle有两种:Alert(对话框), Sheet(上拉菜单)

    UIAlertControllerStyleAlertUIAlertControllerStyleActionSheet

    UIAlertActionStyle 有三种:Default(常规), Cancel(取消), Destructive(警示)

    UIAlertActionStyleDefault, UIAlertActionStyleCancel, UIAlertActionStyleDestructive

    现在看下面枚举的组合例子:

    1 UIAlertControllerStyleAlert (对话框) 添加常规(okAction) 和 取消(cancelAction) 按钮:

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Hi, Warrior!" message:@"Welcome To The Drag World" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:nil];
    
    [alert addAction: okAction];
    
    [alert addAction: cancelAction];
    //呈现视图
    [self presentViewController:alert animated:YES completion:nil];
    
    default & cancel

    2 UIAlertControllerStyleAlert (对话框) 添加常规(okAction) 和警示(destructiveAction) 按钮:

    只需要在上面代码中把UIAlertActionStyleCancel 替换成 UIAlertActionStyleDestructive.

    default & destructive

    3 UIAlertControllerStyleActionSheet (上拉菜单)添加常规,取消和警示按钮, 其中UIAlertActionStyleCancel 的UIAlertAction对象只能有一个,从显示中可以明显看出来:

    UIAlertController* alert = [UIAlertControlleralertControllerWithTitle:@"Hi, Warrior!"message:@"Welcome To The Drag World"preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* okAction = [UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:nil];
    
    UIAlertAction* cancelAction = [UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleCancelhandler:nil];
    
    UIAlertAction* destructiveAction = [UIAlertActionactionWithTitle:@"Cancel"style:UIAlertActionStyleDestructivehandler:nil];
    
    [alertaddAction: cancelAction];
    
    [alertaddAction: okAction];
    
    [alertaddAction: destructiveAction];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    sheet

    4 带文本输入框的对话框: 使用addTextFieldWithConfigurationHandler
    例如:

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Hi, Warrior!" message:@"Welcome To The Drag World" preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            //配置输入框内的占位符
            textField.placeholder=@"Username";
            //设置输入框的背景颜色,其中这里RGB参数的设置范围不是0-255,而是0.0-1.0, alpha 是指背景透明度,0.0为全透明,1.0不透明
            textField.backgroundColor = [UIColor colorWithRed:111.0/255 green:206.0/255 blue:250.0/255 alpha:0.5];
    }];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textFiled){
            textFiled.placeholder=@"Password";
            //设置密码的使用安全文本输入
            textFiled.secureTextEntry=YES;}];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil];
        
        [alert addAction: okAction];
        [alert addAction: cancelAction];
        [alert addAction: deleteAction];
        
        [self presentViewController:alert animated:YES completion:nil ];
    
    带输入框的对话框

    相关文章

      网友评论

        本文标题:UIAlertController 的使用学习

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