UIAlertController是用来代替之前我们使用的UIAlertView和UIActionSheet,这次的改进总体来讲,感觉思路更清晰简洁了,使用起来也是颇为顺手,下面不多说老样子上代码:
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
-
- [self alertViewcontrol];
-
- }
-
- -(void)alertViewcontrol
-
- {
-
-
- UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"司小文的博客" message:@"http://blog.csdn.net/siwen1990" preferredStyle:UIAlertControllerStyleAlert];
-
-
-
-
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- NSLog(@"我是普通按钮");
- }];
-
- UIAlertAction *aaaAction = [UIAlertAction actionWithTitle:@"aaa" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
-
- NSLog(@"我是红色按键");
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- NSLog(@"我是取消按钮");
- }];
-
-
-
- [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
-
-
- textField.text = @"可以在这里写textfield的一些属性";
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listeningTextField:) name:UITextFieldTextDidChangeNotification object:textField];
-
- }];
-
-
- [alertControl addAction:okAction];
- [alertControl addAction:aaaAction];
- [alertControl addAction:cancelAction];
-
-
- [self presentViewController:alertControl animated:YES completion:nil];
-
- }
-
-
- -(void)listeningTextField:(NSNotification *)notionfication
- {
- UITextField *thisTextField = (UITextField*)notionfication.object;
- NSLog(@"%@",thisTextField.text);
- }
-
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
-
- }
在UIAlertController中的显示方式有两种只是在创建时不同,其他的方法基本都是一样的
UIAlertControllerStyleAlert:(有输入框)
UIAlertControllerStyleActionSheet:(无输入框)
在UIAlertController可以添加的按钮总共有三类
UIAlertActionStyleDefault 普通
UIAlertActionStyleDestructive 红色
UIAlertActionStyleCancel 取消
主要记得,在改变UIAlertControllerStyleActionSheet的时候一定不要加上输入框,不然会崩溃呦~
本文为转载 原文地址:http://blog.csdn.net/siwen1990