[一句秒懂] iOS8.0-UIAlertController介绍
2016-06-23 00:00
489 查看
摘要: UIAlertController说明使用
平时我写的拍照demoi 部分:
一 概述 在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView。把两种类型的提示信息放到这一个类里来实现。 注意, 这个class不能通过继承的方式来自定义。 二 类介绍 先举两个使用的例子 例子一 IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)0 UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; //添加Button [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //处理点击拍照 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //处理点击从相册选取 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil]; 例子二 IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)1 实现代码 UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登陆" message: @"输入用户名密码" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"name"; textField.textColor = [UIColor blueColor]; 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]; 三 使用的步骤 第一步 初始化 + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle 这里的preferredStyle有两种,sheet和alert typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } UIAlertControllerStyle; 第二步,添加Action(button或者textfield) 添加Button - (void)addAction:(UIAlertAction *)action 这里的UIAlertAction是一个比较简单的类 + (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler style有三种 typedef enum UIAlertActionStyle: NSInteger { UIAlertActionStyleDefault = 0,//默认 UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive //有可能改变或者数据 } UIAlertActionStyle; 添加TextField 注意,只能是 UIAlertControllerStyleAlert才能添加Textfield - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler 在block里配置textfield的信息,例如placeholder,backgroundcolor等。 Textfield的保存信息可由UIAlertController的属性Textfields获得。如同上述的例子二一样。 第三步,显示 例如 [self presentViewController:alert animated:YES completion:nil];
平时我写的拍照demoi 部分:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择照片来源" preferredStyle:UIAlertControllerStyleActionSheet]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentViewController:imagePickerController animated:YES completion:^{}]; }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) { imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerController animated:YES completion:^{ }]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action){ // 可以不用书写代码 }]; [self presentViewController:alertController animated:YES completion:nil]; //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候我们不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { [alertController addAction:albumAction]; [alertController addAction:photoAction]; [alertController addAction:cancelAction]; }else { [alertController addAction:albumAction]; [alertController addAction:cancelAction]; }
相关文章推荐
- UEditor(百度富文本编辑器)的暴力使用
- String-vs-StringBuilder-vs-StringBuffer
- KVC setvalue:forkey与setvalue:forkeypath的区别
- OpenJudge 1058 Guideposts | BZOJ 3328 PYXFIB
- LeetCode-62-Unique Paths
- 信息无缝滚动效果marquee
- Request.url请求路径的一些属性
- UIKit笔记大全
- iOS学习——UIView圆角图片的设置方法
- APUE(4)---文件和目录 (1)
- 测试基础之易用性测试
- Druid数据库连接池
- Errors occurred during the build.
- Arduino控制直流电机
- Arduino 通过串口传值给Processing 显示
- 【转】自定义UITableViewCell控件阻挡回调不到didSelectRowAtIndexPath的解决办法
- 【转】iOS 通过xib自定义UITableViewCell【原创】
- Java优先队列(PriorityQueue)
- 347. Top K Frequent Elements
- 【转】UITableView详解(UITableViewCell