IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
来源:互联网 发布:win7旗舰版重装软件 编辑:程序博客网 时间:2024/05/02 12:30
一 概述
在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView。把两种类型的提示信息放到这一个类里来实现。
注意, 这个class不能通过继承的方式来自定义。
二 类介绍
先举两个使用的例子
例子一
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionAlert]; //添加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];
例子二
实现代码
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];
总结:
总的来说,API把两种Alertview进行统一,并且不用代理的方式来传递消息。而是改成了block。就个人而言,还是比较喜欢Block的方式的。上述的两幅图的背景请忽略,那是我准备写另一篇关于拍照博客的Demo工程。
0 0
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS8 alertView 和 actionSheet 使用方法
- IOS8 AlertView和ActionSheet不兼容问题解决方案
- iOS8.0前后关于alertView和actionSheet
- ios8之后的UIAlertController详解
- iOS AlertView、ActionSheet
- IOS中的通知--操作表ActionSheet和警报AlertView
- [IOS Code]实现全局关闭所有键盘,actionSheet和alertView
- IOS中的通知--操作表ActionSheet和警报AlertView
- ios8之后的UIAlertController
- iOS8之后UIAlertController
- Actionsheet和AlertView的使用
- ActionSheet和AlertView的封装
- IOS8之后ActionSheet和ActionView合体,简单更好用
- iOS8 AlertView 改变 引入了UIAlertController
- 【郑轻】[1776]和尚特烦恼2——第几个素数
- 基于安卓客户端的百度云消息推送实现DEMO
- hdu2553 N皇后问题--DFS
- Newsgroup18828文本分类器、文本聚类器、关联分析频繁模式挖掘算法的Java实现工程下载及运行FAQ
- OS学习之文件系统
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- centos 网络配置
- AppInfoUtils
- CRickEditView 取消自动换行
- 浅谈Java SE、Java EE、Java ME三者的区别
- Canvas基础知识总结之二
- frozen
- 数据挖掘-基于贝叶斯算法及KNN算法的newsgroup18828文本分类器的JAVA实现(上)
- 杀毒Utils