UIAlertContronller的使用
来源:互联网 发布:实战nginx pdf 编辑:程序博客网 时间:2024/05/29 11:06
苹果公司已经建议开发使用UIAlertController来替代UIActionSheet和UIAlertView,把这两个合并到了一个类里来实现他们,所以我们跟上潮流!下面总结一下!
老规矩,直接上代码,不废话!
这段代码的背景是选取系统的照片
- (void)setActionSheet{ //初始化UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; /* UIAlertControllerStyle有两种,一种是UIAlertControllerStyleActionSheet(相当于UIActionSheet),另一种是UIAlertControllerStyleAlert(相当于UIAlertView) */ //确定了UIAlertController的style,我们要添加action 了(button或者textfield) [alertController addAction:[UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //处理拍照 }]]; /* 在添加UIAlertAction时,要选它的style! UIAlertAction的style有以下3种: UIAlertActionStyleDefault = 0,//默认 UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive //有可能改变或者数据 */ [alertController addAction:[UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //处理相册 }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alertController animated:YES completion:nil];}
到这里,一个“UIActionSheet”就可以运行了!
下面说下“UIAlertView”!
- (void)setAlertView{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录" message:@"请输入帐户密码" preferredStyle:UIAlertControllerStyleAlert];//注意这里的UIAlertController的style是UIAlertControllerStyleAlert,而不是UIAlertControllerStyleActionSheet //Textfield的添加 注意:只能是 UIAlertControllerStyleAlert才能添加Textfield! [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"please input your name"; textField.textColor = [UIColor blackColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"password"; textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; textField.secureTextEntry = YES; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSArray * textfields = alertController.textFields; UITextField * namefield = textfields[0]; UITextField * passwordfiled = textfields[1]; NSLog(@"%@:%@",namefield.text,passwordfiled.text); }]]; [self presentViewController:alertController animated:YES completion:nil];}
说下另一种情况的“UIAlertView”!
代码写在了懒加载里
- (UIAlertController *)alertController{ if (!_alertController) { _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您是否确认提交?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //在这里写确认后你要干的事. }]; [_alertController addAction:cancelAction]; [_alertController addAction:okAction]; } return _alertController;}
到这里你应该掌握了吧!
参考了大神的资料,如果有不对之处,希望批评指正!
我是菜鸟,但我有一颗大神的心!
0 0
- UIAlertContronller的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- 正则表达式 匹配规则(五)
- ibatis出现Mapped Statements collection already contains value for 错误
- 51nod 1072 博弈 威佐夫游戏
- php中判断shell_exec执行结果
- 剑指offer之面试题27:二叉搜索树与双向链表
- UIAlertContronller的使用
- Node.js 学习笔记 —— 概述
- ssh整合——Hibernate出错 HQL语句出错
- spring+hibernate+jta分布式事务Demo
- 正则表达式(六)
- HTML TextArea的值保存后显示错误问题。
- 修改Android Studio的代码补全快捷键,解决与输入法快捷键冲突
- 数据结构与算法——最短路径Dijkstra算法的C++实现
- caffe SigmoidLayer 学习