UIAlertContronller的使用
2016-05-04 15:41
459 查看
苹果公司已经建议开发使用UIAlertController来替代UIActionSheet和UIAlertView,把这两个合并到了一个类里来实现他们,所以我们跟上潮流!下面总结一下!
老规矩,直接上代码,不废话!
这段代码的背景是选取系统的照片
到这里,一个“UIActionSheet”就可以运行了!
下面说下“UIAlertView”!
说下另一种情况的“UIAlertView”!
代码写在了懒加载里
到这里你应该掌握了吧!
参考了大神的资料,如果有不对之处,希望批评指正!
我是菜鸟,但我有一颗大神的心!
老规矩,直接上代码,不废话!
这段代码的背景是选取系统的照片
- (void)setActionSheet{ //初始化UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; /* UIAlert 4000 ControllerStyle有两种,一种是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; }
到这里你应该掌握了吧!
参考了大神的资料,如果有不对之处,希望批评指正!
我是菜鸟,但我有一颗大神的心!
相关文章推荐
- iOS开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- iOS开发使用JSON解析网络数据
- IOS开发代码分享之获取启动画面图片的string
- iOS开发实现音频播放功能
- iOS开发之视图切换
- iOS开发使用XML解析网络数据
- IOS开发实现录音功能
- iOS开发之UIScrollView详解
- IOS开发之路--C语言数组和字符串
- iOS开发之触摸事件
- IOS开发代码分享之用nstimer实现倒计时功能
- iOS开发使用GDataXML框架解析网络数据
- iOS开发之UITableView详解
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios推送证书
- iOS和tvOS游戏按需加载资源简介
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结