UIAlertController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

for (int i = 0 ; i < 2; i ++) {
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(120 + i * 120, 120, 100, 50);
button.backgroundColor = [UIColor lightGrayColor];
if (i == 0) {
[button setTitle:@"alertView" forState:UIControlStateNormal];
}else{
[button setTitle:@"actionSheet" forState:UIControlStateNormal];
}
button.tag = 10 + i ;
[button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];
}

}
-(void)onClick:(UIButton*)button{

if (button.tag == 10) {
NSLog(@"alertView");
[self alertView];
}else{
NSLog(@"actionSheet");
[self actionSheet];
}
}
-(void)actionSheet{
// 初始化UIAlertCOntroller
// Title:标题
// message:信息
// Style:样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

// 初始化UIAlertAction
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"取消");
}];

UIAlertAction *firstBtn = [UIAlertAction actionWithTitle:@"first" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"取消");
}];



[alertController addAction:cancelBtn];
[alertController addAction:firstBtn];
[self presentViewController:alertController animated:YES completion:nil];

}
-(void)alertView{
// 初始化UIAlertController
// Title:标题
// message:信息
// Style:样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertControllerStyleActionSheet
// UIAlertControllerStyleAlert

// 初始化按钮
// Title:标题
// style:样式
// UIAlertController 上面只能有一个取消样式的按钮,否则会崩溃
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"取消");
}];
// UIAlertActionStyleDefault = 0,普通按钮
// UIAlertActionStyleCancel,取消按钮
// UIAlertActionStyleDestructive重置、销毁

// 添加文本输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"请输入";
}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = @"第二个文本输入框";
}];

UIAlertAction *resetBtn = [UIAlertAction actionWithTitle:@"reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {

UITextField *textField1 = alertController.textFields[0];
NSLog(@"%@",textField1.text);

UITextField *textField2 = alertController.textFields[1];
NSLog(@"%@",textField2.text);
}];


// 添加动作
[alertController addAction:cancelBtn];
[alertController addAction:resetBtn];


// 模态
[self presentViewController:alertController animated:YES completion:nil];
}
@end