UIAlertController的使用
在iOS8中,苹果对UIAlertView和UIActionSheet进行了重新的封装,成为适应性更强,灵活性更高的UIAlertController。具体使用方法如下。
UIAlertController具有两种风格,即
UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert
至于其使用方法也是非常简单得 ,代码如下
UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"警告框" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"警示性" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"不确定");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消");
}]];
[self presentViewController:al animated:YES completion:nil];
以上代码,既可以实现UIAlertView也可以实现ActionSheet, 只需要修改初始化的第三个参数,便可实现相应得弹窗。
如果是UIAlertControllerStyle当然还可以添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder=@"一个文本框";
}];
如果要读取文本框中的内容
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField * textField=al.textFields.firstObject;
NSLog(@"%@",textField.text);
}]];
以上便是UIAlertController的使用方法,但是,当我们在使用UIAlertControllerStyleActionSheet时需要注意,通过以上方法,我们将actionSheet运行在iPhone这样的设备上是没有问题得,但是如果运行在大屏得ipad上便会出现崩溃。 这是因为弹出框必须要有一个锚点,即 anchor point。当我们在iPhone等常规屏幕的设备上使用actionSheet时,很明显,锚点在屏幕下方,而在大屏得ipad上,则需要我们自己手动设置。只需要加入以下代码
UIPopoverPresentationController * popover=al.popoverPresentationController;
UIButton * button=(UIButton *)sender;
if (popover) {
popover.sourceRect=button.bounds;
popover.sourceView=button;
popover.permittedArrowDirections=UIPopoverArrowDirectionAny;
}
另外,当我们在使用此方法后,ipad上加入的UIAlertActionStyleCancel风格的按钮是不显示,即默认去掉了取消按钮。
UIAlertController的使用的更多相关文章
- UIAlertController
楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...
- iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法
今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...
- UIAlertController使用
// 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController // 参数preferredStyle: ...
- IOS UIAlertController 使用方法
在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, ...
- UI控件(UIAlertController)
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...
- UIAlertController 部分用法及属性
//创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...
- iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...
- 开始使用 UIAlertController 吧
UIAlertView 与 UIActionSheet UIAlertView 样式 实现 - (void)showAlertView { self.alertView = [[UIAlertView ...
- UI第十四节——UIAlertController
- (void)viewDidLoad { [super viewDidLoad]; UIButton *alertBtn = [UIButton buttonWithType:U ...
- iOS 8.0后使用UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
随机推荐
- Python学习之路——基础篇(1)字符串格式化
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 百分号方式 ...
- 极光推送NotificationServiceTest.appex文件变红不存在丢失解决办法
这个图片是文件丢失的 创建.appex文件 创建.appex文件 把极光demo里面的这两个文件复制粘贴到新的文件件(新创建的文件是 UIViewController类型的所以就把 (Notifica ...
- java解析中国行政区域并在页面显示实现动态逐级筛选
一.实现目标 首先会有一个存放中国行政区域数据的一个txt文件,用java读取并解析出来,并在页面上通过下拉框的形式展示出来.实现效果如下图,当选择完省份后,在选择该省份下的城市,然后在选择该城市下的 ...
- Android 7.0 UICC 分析(三)
本文讲解UICCCardApplication /frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/UiccC ...
- ASP.NET MVC4中@model使用多个类型实例的方法
转http://blog.csdn.net/hulihui/article/details/48199897
- PLSQLDeveloper 提示不能初始化?
原因: oracle数据库是64位的,而 PLSQL Developer 只有32位的! 下载PLSQL_Developer地址: http://pan.baidu.com/share/link?sh ...
- Git常用命令学习(2)
1):git branch -v --查看每一个分支的最后一次提交2):git branch --merged 与 --no-merged 这两个有用的选项可以过滤这个列表中已经合并或尚未合并到当前分 ...
- 判断当前VC是不是正在展示的活跃页面
viewController.isViewLoaded && viewController.view.window
- [问题]数据库MySQL和Navicat的乱码问题
计算机中存储字符需要使用编码集,早期有ASCII集,但是随着技术的发展,ASCII集不能满足需求,出现了越来越多的字符,比如中文字符等.后来又发展出了Unicode.GB2312.utf8等字符集.字 ...
- NC 解决启动环境报内存溢出问题
java heap space 内存溢出 解决方法如下: 在eclipse中,window-->preferences-->Java-->Installed JREs选中JRE 点击 ...