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在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
随机推荐
- Endless Sky源码学习笔记-3
文本解析: 将txt存入DataFile,DataFile包含DataNode,每个DataNode是txt中的一行,每个DataNode包含若干token(std::vector<string ...
- POJ 3624 Charm Bracelet(01背包)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34532 Accepted: 15301 ...
- SQL中CONVERT日期不同格式的转换用法
SQL中CONVERT日期不同格式的转换用法 格式: CONVERT(data_type,expression[,style]) 说明:此样式一般在时间类型(datetime,smalldatetim ...
- NPOI 2.0版本的使用
详细教程: http://blog.csdn.net/xxs77ch/article/details/50216033 using System; using System.Collections.G ...
- 你不知道的函数节流,提高你的JS性能!
浏览器的DOM计算处理非常耗费CPU时间,霸占内存,这对我们的开发来说是非常不友好的,,比如IE浏览器的onresize事件就可能在用户稍微拖动一下窗口时计算上千次,甚至更高频率直接让浏览器崩溃... ...
- Finder Item脚本如何包装成 Mac App
第一步,包装 记得之前把一个 java 程序的 jar 包 用 automator包装成了 app, 但自己找不到做法了.回头我想了想 这么实验可行. 这样做成 app 后保存在 /Applicati ...
- RPC学习----Thrift快速入门和Java简单示例
一.什么是RPC? RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. RPC协议 ...
- List集合基于某个字段排序
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...
- Windows10
一.快速开机设置 我的电脑配置如图,装有VS2015 2010 OFFICE等常用开发工具,在线升级后开机速度并没有明显提升. 1.保证windows font cache service服务启动,3 ...
- MongoDB的TruncationException异常解决方法
近期由于DB4O的一些无解的BUG,导致现行的项目查询效率底下,于是愤而迁移到了MongoDB. MongoDB虽然比DB4O的用户要多一些,但是文档依然极度匮乏,遇到问题不是那么容易就能搜到解决办法 ...