1 //
2 // ViewController.m
3 // UIAlertController详解
4 //
5 // Created by 大欢 on 16/1/25.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()
12
13 - (IBAction)showAlertController:(id)sender;
14
15 @end
16
17 @implementation ViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21
22 }
23
24 - (IBAction)showAlertController:(id)sender {
25
26 //创建UIAlertController
27
28 UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"题目" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
29
30 //创建按钮
31 UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
32 NSLog(@"no");
33 }];
34
35 UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
36 NSLog(@"ok");
37
38 UITextField * field = alertController.textFields[0];
39 NSLog(@"%@",field.text);
40 }];
41
42 //添加文本
43 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
44 textField.secureTextEntry = YES;
45 }];
46
47 [alertController addAction:action1];
48 [alertController addAction:action2];
49
50 //展示alertController
51 [self presentViewController:alertController animated:YES completion:nil];
52
53 }
54
55 @end
![]()