UIAlertController 警告框

switch (button.tag) {
        case 101:
        {
            /*
             按钮的类型
             UIAlertActionStyleDefault = 0,
             UIAlertActionStyleCancel,
             UIAlertActionStyleDestructive

             */
            //弹出警告框UIAlertControllerStyleAlert
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"非法入侵" preferredStyle:UIAlertControllerStyleAlert];
            //增加 按钮 行为
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                //这个block 点击取消按钮 会 回调的 代码块
                NSLog(@"alert 取消被点击");
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"alert 确定被点击");
            }]];
            //只能模态跳转
            [self presentViewController:alert animated:YES completion:nil];
            
        }
            break;
        case 102:
        {
            //UIAlertControllerStyleActionSheet
            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"分享" message:@"看这里。。。" preferredStyle:UIAlertControllerStyleActionSheet];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"QQ" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"QQ被点击");
            }]];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"人人" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"人人被点击");
            }]];
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"sina" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"sina被点击");
            }]];
            //取消
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
            }]];
            //删除
            [actionSheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                NSLog(@"删除被点击");
            }]];
            //模态跳转
            [self presentViewController:actionSheet animated:YES completion:nil];
            
        }
            break;
        case 103:
        {
            //alert 自带 textField
            UIAlertController *alertText = [UIAlertController alertControllerWithTitle:@"登录" message:@"登录账号" preferredStyle:UIAlertControllerStyleAlert];
            //增加textField
            [alertText addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                //这个block 初始化 alert的时候回调 可以对textField进行设置
                textField.placeholder = @"请输入用户名";
            }];
            [alertText addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                //这个block 初始化 alert的时候回调 可以对textField进行设置
                textField.placeholder = @"请输入密码";
                textField.secureTextEntry = YES;
            }];
            //增加按钮
            [alertText addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消被点击");
            }]];
            [alertText addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //打印 textField的内容
                //alertText.textFields获取 textfield数组
                NSLog(@"name:%@",[alertText.textFields[0] text]);
                NSLog(@"passwd:%@",[alertText.textFields[1] text]);
            }]];
            
            [self presentViewController:alertText animated:YES completion:nil];
        }
            break;

原文地址:https://www.cnblogs.com/cdp-snail/p/4918595.html