iOS开发 - UIAlertController 弹框提醒
2015-08-30 11:51
525 查看
传统的alertView
- (void)alertView { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert show]; }
传统的actionSheet
- (void)actionSheet { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"关闭", nil]; [sheet showInView:self.view]; }
UIAlertController == UIAlertView + UIActionSheet
- (void)alertController { // 危险操作:弹框提醒 // 1.UIAlertView // 2.UIActionSheet // iOS8开始:UIAlertController == UIAlertView + UIActionSheet UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleAlert]; // 添加按钮 __weak typeof(alert) weakAlert = alert; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"点击了取消按钮"); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"点击了其它按钮"); }]]; // 添加文本框 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.textColor = [UIColor redColor]; textField.text = @"123"; [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.secureTextEntry = YES; textField.text = @"123"; }]; [self presentViewController:alert animated:YES completion:nil]; } - (void)usernameDidChange:(UITextField *)username { NSLog(@"%@", username.text); }
其他操作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleActionSheet]; // 设置popover指向的item alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem; // 添加按钮 [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"点击了确定按钮"); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"点击了取消按钮"); }]]; [self presentViewController:alert animated:YES completion:nil]; } // UIAlertControllerStyleActionSheet的使用注意 // 1.不能有文本框 // 2.在iPad中,必须使用popover的形式展示 // Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert // 只能在UIAlertControllerStyleAlert样式的view上添加文本框
相关文章推荐
- 考评系统学到的UI设计理念
- UIWindow和UIView
- UI设计规范
- iOS8新特性(2)——UIPopoverController和UIPresentationController
- iOS8新特性(1)——UIAlertController
- 编写广播时出现Exported receiver does not require permission
- 学习YUI.Ext 第一天:EXT简介(一)
- HOWTO: Install, Build and Use openCV (MacOSX 10.10)
- POJ 2524 Ubiquitous Religions(宗教种类:并差集)
- adbd cannot run as root in production builds
- iphone开发之表格组件UITableView的使用(一)使用时的具体步骤介绍
- UUID.randomUUID().toString 简单解释
- CodeForces 441A Valera and Antique Items
- iOS开发 - UICollectionViewLayout 自定义布局
- iOS开发 - UICollectionViewFlowLayout 流水布局
- UIday02~04:UITextField UIButton UIdelegate 自定义视图 容器视图控制器
- POJ 3458 && HDU 1683 Colour Sequence(水~)
- Win10 Build 10532语言包官方下载地址汇总
- POJ 3094 && HDU 2734 Quicksum(水~)
- esayyui的layout嵌套布局可以解决fit自适应的位置偏移问题