iOS8中UIAlertController的使用

iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,将它们集合成了自己的style;

1------------UIAlertControllerStyleAlert-----原有的UIAlertView

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleAlert];

//preferredStyle--只读

//打印选择的AlertController的类型 actionSheet--0 alertView--1

NSLog(@"%ld",alertC.preferredStyle);

//获取alertC的标题和信息

NSLog(@"%@",alertC.title);

NSLog(@"%@",alertC.message);

//更改标题

alertC.title = @"title change";

//直接给alertC添加事件执行按钮actionTitle(只能添加一个)

/*

[alertC addAction:[UIAlertAction actionWithTitle:@"actionTitle" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

//点击actionTitle时的回调方法

}]];

*/

//添加多个按钮

//没有任何代理的情况下,在我们点击alertView界面中的按钮时,就不需要用buttonIndex来区分我们点击的是哪个按钮,同时一个界面上多个alertView时,也不需要用tag值来区分你到底点击的是哪一个alertView上的按钮了

//这样便于我们的逻辑思维,但是代码量并没有减少

UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

style:UIAlertActionStyleCancel

handler:^(UIAlertAction *action) {

//cancle对应执行的代码

NSLog(@"cancle action");

}];

UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

style:UIAlertActionStyleDefault

handler:^(UIAlertAction *action) {

//sure对应执行的代码

NSLog(@"sure action");

}];

[alertC addAction:action];

[alertC addAction:action1];

//alertView的输入框 sheet类型中没有 但是sheet类型的UIAlertController可以添加textField 当用户要显示sheet时程序会崩

[alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

textField.placeholder = @"textFiled";

}];

//在这里不管是alertView还是actionSheet都是一个独立的Cotroller,所以这里需要通过presentViewController来展现我们的alertView或者actionSheet

[self presentViewController:alertC animated:YES completion:nil];

2---------------UIAlertControllerStyleActionSheet----原有的UIActionSheet

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")

style:UIAlertActionStyleCancel

handler:^(UIAlertAction *action) {

NSLog(@"cancle action");

}];

UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")

style:UIAlertActionStyleDefault

handler:^(UIAlertAction *action) {

NSLog(@"sure action");

}];

[alertC addAction:action];

[alertC addAction:action1];

[self presentViewController:alertC animated:YES completion:nil];

时间: 2024-08-25 03:40:36

iOS8中UIAlertController的使用的相关文章

iOS -Swift:如何使用iOS8中的UIAlertController

1.前言 在前段时间手机QQ:升级iOS8.3后,发图就崩的情况, 就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的 具体原因分析 这个可以看腾讯团队发出来的总结分享. http://bugly.qq.com/blog/?p=135# 在Xcode头文件中苹果也明确给出用UIAlertController替代UIAlertview和UIActionSheet的标识 所以iOS8以后我们还是使用苹果推荐的UIAlertController吧(这货居然是

iOS 学习笔记 九 (2015.04.02)IOS8中使用UIAlertController创建警告窗口

1.IOS8中使用UIAlertController创建警告窗口 #pragma mark - 只能在IOS8中使用的,警告窗口- (void)showOkayCancelAlert{    NSString *title = NSLocalizedString(@"修改组名", nil);    NSString *message = NSLocalizedString(@"请输入新的组名", nil);    NSString *cancelButtonTitl

iOS8开发之iOS8的UIAlertController

在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSheet可以这样写: UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title,nil时不显示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherB

iOS8中的定位服务

My app that worked fine in iOS 7 doesn't work with the iOS 8 SDK. CLLocationManager doesn't return a location, and I don't see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wro

2015/10/3 iOS 笔记 细节 iOS9中UIAlertController的简单使用 ScrollView NSTimer

一.iOS9中UIAlertController的简单使用 很明显,简单的UIAlertView已经不能用了,我感觉很伤心. // 创建 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开始了" message:@"开始了!" preferredStyle:UIAlertControllerStyleActionSheet]; // UIAlertControllerS

初探 iOS8 中的 Size Class

以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死,心里真替他们高兴.不过在做到 iPhone 和 iPad 的适配的时候,一个页面需要配置多个 xib 进行开发还是个很头疼的事情.再加上 iPhone6 和 iPhone6 plus 的发布,适配似乎也变得麻烦起来.今天了解了 iOS8 中的 Size Class 之后,真的笑,笑出声. 简介 先来看一下我们的新伙伴:Size Classes.在 iOS8 中,我们不用再像以前那样,一个页面新建多

iOS8中使用CoreLocation定位

在iOS8中,苹果已经强制开发者在请求定位服务时获得用户的授权,此外iOS状态栏中还有指示图标,提示用户当前应用是否正在使用定位服务.另外在iOS8中,苹果进一步改善了定位服务,让开发者请求定位服务时需要向用户提供更多的透明.此外,iOS8中还支持让应用开发者调用全新的"访问监控"功能,当用户允许后应用才能获得更多的定位数据. iOS8以前使用CoreLocation定位 1.首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.frame

[iOS] 初探 iOS8 中的 Size Class

转自:http://www.itnose.net/detail/6112176.html ? ? ? 以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死,心里真替他们高兴.不过在做到 iPhone 和 iPad 的适配的时候,一个页面需要配置多个 xib 进行开发还是个很头疼的事情.再加上 iPhone6 和 iPhone6 plus 的发布,适配似乎也变得麻烦起来.今天了解了 iOS8 中的 Size Class 之后,真的笑,笑出声. 简介 先

iOS8中添加的extensions总结(一)——今日扩展

通知栏中的今日扩展 分享扩展 Action扩展 图片编辑扩展 文件管理扩展 第三方键盘扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutorials> 关于App extensions 的原理,即How extensions work 首先App扩展是一个App功能性上的扩展,它并不独立与你原来的App,也就是说在给App Store提交的时候是打包到原有App中一起提交,它们并不是独立的App.其次,App的每一种扩展都有自己单独的API