1. 最简单的提醒视图:
- -
(IBAction)doAlert:(id)sender { -
// 准备初始化配置参数 -
NSString *title = @"Alert Button Selected"; -
NSString *message = @"I need your attention NOW!"; -
NSString *okButtonTitle = @"OK"; -
-
// 初始化 -
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle :title message:message preferredStyle:UIAlertControllerStyleAl ert]; -
-
// 创建操作 -
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefaul t handler:^(UIAlertAction *action) { -
// 操作具体内容 -
// Nothing to do. -
}]; -
-
// 添加操作 -
[alertDialog addAction:okAction]; -
-
// 呈现警告视图 -
[self presentViewController:alertDialog animated:YES completion:nil]; - }
![在iOS <wbr>8中使用UIAlertController](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
2. 多个按键的提醒视图
- -
(IBAction)doMultiButtonAlert:(id)sender { -
// 准备初始化配置参数 -
NSString *title = @"Alert Button Selected"; -
NSString *message = @"I need your attention NOW!"; -
NSString *okButtonTitle = @"OK"; -
NSString *neverButtonTitle = @"Never"; -
NSString *laterButtonTitle = @"Maybe Later"; -
-
// 初始化 -
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle :title message:message preferredStyle:UIAlertControllerStyleAl ert]; -
-
// 分别3个创建操作 -
UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefaul t handler:^(UIAlertAction *action) { -
// 普通按键 -
self.userOutput.text = @"Clicked 'Maybe Later'"; -
}]; -
UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestru ctive handler:^(UIAlertAction *action) { -
// 红色按键 -
self.userOutput.text = @"Clicked 'Never'"; -
}]; -
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { -
// 取消按键 -
self.userOutput.text = @"Clicked 'OK'"; -
}]; -
-
// 添加操作(顺序就是呈现的上下顺序) -
[alertDialog addAction:laterAction]; -
[alertDialog addAction:neverAction]; -
[alertDialog addAction:okAction]; -
-
// 呈现警告视图 -
[self presentViewController:alertDialog animated:YES completion:nil]; - }
![在iOS <wbr>8中使用UIAlertController](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
3. 带输入框的提醒视图
- -
(IBAction)doAlertInput:(id)sender { -
// 准备初始化配置参数 -
NSString *title = @"Email Address"; -
NSString *message = @"Please enter your your email address:"; -
NSString *okButtonTitle = @"OK"; -
-
// 初始化 -
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle :title message:message preferredStyle:UIAlertControllerStyleAl ert]; -
-
// 创建文本框 -
[alertDialog addTextFieldWithConfigur ationHandler:^(UITextField *textField){ -
textField.placeholder = @"Your Email"; -
textField.secureTextEntry = NO; -
}]; -
-
// 创建操作 -
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefaul t handler:^(UIAlertAction *action) { -
// 读取文本框的值显示出来 -
UITextField *userEmail = alertDialog.textFields.firstObject; -
self.userOutput.text = userEmail.text; -
}]; -
-
// 添加操作(顺序就是呈现的上下顺序) -
[alertDialog addAction:okAction]; -
-
// 呈现警告视图 -
[self presentViewController:alertDialog animated:YES completion:nil]; - }
![在iOS <wbr>8中使用UIAlertController](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
4. 提醒图表
- -
(IBAction)doActionSheet:(id)sender { -
// 准备初始化配置参数 -
NSString *title = @"Alert Button Selected"; -
NSString *message = @"I need your attention NOW!"; -
NSString *okButtonTitle = @"OK"; -
NSString *neverButtonTitle = @"Never"; -
NSString *laterButtonTitle = @"Maybe Later"; -
-
// 初始化 -
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle :title message:message preferredStyle:UIAlertControllerStyleAc tionSheet]; -
-
// 分别3个创建操作 -
UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefaul t handler:^(UIAlertAction *action) { -
// 普通按键 -
self.userOutput.text = @"Clicked 'Maybe Later'"; -
}]; -
UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestru ctive handler:^(UIAlertAction *action) { -
// 红色按键 -
self.userOutput.text = @"Clicked 'Never'"; -
}]; -
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { -
// 取消按键 -
self.userOutput.text = @"Clicked 'OK'"; -
}]; -
-
// 添加操作(顺序就是呈现的上下顺序) -
[alertDialog addAction:laterAction]; -
[alertDialog addAction:neverAction]; -
[alertDialog addAction:okAction]; -
-
// 呈现警告视图 -
[self presentViewController:alertDialog animated:YES completion:nil]; - }
![在iOS <wbr>8中使用UIAlertController](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)