UIAlertAction 能jk触发器的功能功能吗

UIAlertController的集成关系:
UIAlertController:UIViewController:UIResponder:NSObject
-(void)payBtnAction
+ actionWithTitle:style:handler:
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
2、获取Action属性
//获取标题(只读)
//获取类型(只读)
UIAlertActionStyleDefault默认、 Cancel取消、 Destructive销毁
//是否可用
NSLog(@"title:%@",
cancelAction.title);
NSLog(@"style:%ld",(long)cancelAction.style);
cancelAction.enabled = NO;
UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了完成按钮");
+ alertControllerWithTitle:message:preferredStyle:
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
2、配置Alert
//设置和获取标题
//设置和获取消息
.preferredStyle
//类型(只读):UIAlertControllerStyleActionSheet,UIAlertControllerStyleAlert
alert.title = @"Doudou Alert";
alert.message = @"This is Doudou's alert";
3、配置Action
- addAction:
//添加Action
//获取Actions数组(只读)
[alert addAction:cancelAction];
[alert addAction:doneAction];
4、配置TextField
- addTextFieldWithConfigurationHandler: //添加输入框,block中配置输入框的属性
.textFields
//通过[[alert.textFields objectAtIndex:0] text]获取输入框中的内容
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"Login";
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"Password";
NSLog(@"%@",[[alert.textFields objectAtIndex:0] text]);
//父类中的方法,此方法不能写在viewDidLoad中
[self presentViewController:alert animated:YES completion:nil];
本文已收录于以下专栏:
相关文章推荐
-(void)addPicsBtnClick{
UIAlertController*alertController = [[UIAlertController alloc] init];
第一步:UIAlertController对象
UIAlertController *alert = [UIAlertController alertControllerWithTitle:...
iOS 8.0 添加了一个新的类UIAlertController用来取代曾经的
#import &ViewController.h&
@interface ViewController ()
@property (nonatomic,strong) UIButton *...
iOS开发: AlertController与AlertAction 使用方法
猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网--,谢谢!
原文地址: /?p=252
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解:
一、UIAlertView与UIAlertController是什么东东?
二、我们为什么要用UIAlertView或UIAlertController?
三、如何使用UIAlertView和UIAlertController?
四、阅读提醒。
一、UIAlertView与UIAlertController是什么东东?
&一句话,在所有移动应用中,出现的提示窗一般都由UIAlertView或UIAlertController来编写的,两者功能相同。
二、我们为什么要用UIAlertView或UIAlertController?&
  好的人机交互,可以提高用户体验,操作系统的人机交互功能是决定计算机系统&友善性&的一个重要因素。人机交互功能主要靠可输入输出的外部设备和相应的软件来完成。在传统时期,可供人机交互使用的设备主要有键盘显示、鼠标、各种模式识别设备等。与这些设备相应的软件就是操作系统提供人机交互功能的部分。早期的人机交互设施主要是键盘和显示器。操作员通过键盘打入命令,操作系统接到命令后立即执行并将结果通过显示器显示。打入的命令可以有不同方式,但每一条命令的解释是清楚的,唯一的。如今,在移动端我们所进行的所有操作均是在可触摸屏幕上进行的,为了更好的进行人机交互,工程师便把操作信息或提示信息显示到屏幕上,用户根据自身判断来决定所需点击的按钮。一句话,就是利用人机交互更好的提升用户的使用体验和产品设计的质量。
三、如何使用UIAlertView和UIAlertController?
&1、UIAlertView的使用方法:
// Newly initialized alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Edit" message:@"Please Modify the Info" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", @"Other", nil];
// 为下面修改数据用
// This property is inherited from the UIView, You can use this property to distinguish when a AlertView has multiple view
alertView.tag = indexPath.
// Adds a button to the receiver with the given title.
[alertView addButtonWithTitle:@"addBtn"];
UIAlertViewStyle = 以下4种
UIAlertViewStyleDefault,
UIAlertViewStyleSecureTextInput,
//密码输入框风格
UIAlertViewStylePlainTextInput,
//普通输入框风格
UIAlertViewStyleLoginAndPasswordInput
//账号密码框风格
// An alert that allows the user to enter text. Available in iOS 5.0 and later.
alertView.alertViewStyle = UIAlertViewStylePlainTextI
// Returns the text field at the given index
UITextField *textField = [alertView textFieldAtIndex:0];
textField.text = model.
// The number of buttons on the alert view. (read-only)
NSLog(@"The total number of button is : %ld", alertView.numberOfButtons);
// Returns the title of the button at the given index.
NSLog(@"The button title at the specified index : %@", [alertView buttonTitleAtIndex:1]);
// The index number of the cancel button.
NSLog(@"Index for the cancel button is : %ld",alertView.cancelButtonIndex);
// -1 if no otherButtonTitles or initWithTitle:... not used
NSLog(@"The index of the first other button is (read-only) : %ld",alertView.firstOtherButtonIndex);
// show UIAlertView
[alertView show];
UIAlertView的一些基本属性
//根据被点击按钮的索引处理点击事件
Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonI
//AlertView即将显示时
-(void)willPresentAlertView:(UIAlertView *)alertV
// AlertView已经显示时的事件
-(void)didPresentAlertView:(UIAlertView *)alertV
// ALertView即将消失时的事件
// This method is invoked before the animation begins and the view is hidden.
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonI
// AlertView已经消失时执行的事件
// This method is invoked after the animation ends and the view is hidden.
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonI
// AlertView的取消按钮的事件
// If not defined in the delegate, we simulate a click in the cancel button
-(void)alertViewCancel:(UIAlertView *)alertV
&2、UIAlertController的使用方法:
  *& 使用UIAlertController共需要三步
&& & *& 1.实例化alert:alertControllerWithTitle
&& & *& 2.实例化按钮:actionWithTitle
&& & *& 3.显示alertController:presentViewController
// 1.实例化alert:alertControllerWithTitle
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"编辑" message:@"请修改菜单名称:" preferredStyle:UIAlertControllerStyleAlert];
// 2.实例化按钮:actionWithTitle
// 为防止block与控制器间循环引用,我们这里需用__weak来预防
__weak typeof(alert) wAlert =
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 点击确定按钮的时候, 会调用这个block
NSLog(@"%@",[wAlert.textFields.firstObject text]);
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
// 添加文本框(只能添加到UIAlertControllerStyleAlert的样式,如果是preferredStyle:UIAlertControllerStyleActionSheet则会崩溃)
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.text = model.
//监听文字改变的方法
[textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
// 密文形式显示
textField.text = model.
// 3.显示alertController:presentViewController
[self presentViewController:alert animated:YES completion:nil];
UIAlertController的基本使用
四、阅读提醒
  在Xcode的iOS8 SDK中,UIAlertView和UIAlertController都被UIAlertController取代。官方库解释:"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead."、"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of&UIAlertControllerStyleActionSheet instead."。说明了在iOS8+开发,UIALertView和UIActionSheet已经过时了,UIAlertController以一种模块化替换的方式来代替这两这两个控件的功能和作用。
阅读(...) 评论()UIAlertController使用 - 简书
UIAlertController使用
昨天苹果正式推送的iOS9。当天在网上就看到了很过开发朋友就在说可以放弃iOS7了(当然还要支持iOS6的朋友们不要哭)。我们基本遵守支持最新的2-3个iOS版本。如今iOS9正式推送后,估计大部分开发朋友在不久的将来也要最低支持iOS8了。那么,上面的标题种UIAlertController就是在iOS8推出的新类。UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet,相比后面两个100%的开发朋友使用过.先简单看下UIAlertView和UIActionSheet的使用和效果1.UIAlertView使用:
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"我是最牛逼的开发"delegate:nilcancelButtonTitle:@"不是"otherButtonTitles:@"是",nil];alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordI[alert show];
UIAlertView
2.UIActionSheet使用
UIActionSheet*sheet = [[UIActionSheetalloc]initWithTitle:@"我是最牛逼的开发"delegate:nilcancelButtonTitle:@"是"destructiveButtonTitle:@"是" otherButtonTitles:@"关闭",nil];[sheet showInView:self.view];
UIActionSheet
以上的使用,在熟悉不过了,就不多说了。那么说说UIAlertController吧,直接先看看使用,然后再说说区别3.UIAlertController使用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的发"preferredStyle:UIAlertControllerStyleAlert];当你想 show出来的时候,进入UIAlertController.h文件发现该类没有 show方法,但是看到@interface UIAlertController :UIViewController
从这就看出来该类是一个控制器。那么我们就可以试着想普通展示一个UIViewController的方法试一试,在这我使用的是model[selfpresentViewController:alertanimated:YEScompletion:nil];
运行看效果:
UIAlertController
这个样子,傻眼了,它挡住了整个屏幕,又没有关闭按钮,导致无法关闭。那么如何添加像UIAlertView一样添加一些取消或者其他按钮。UIAlertController是通过- (void)addAction:(UIAlertAction*) 这样添加一个action就是添加一个按钮事件,就像这样:[alertaddAction [UIAlertActionactionWithTitle:@"定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*action) {
NSLog(@"点击了确定按钮"); }]];[alertaddAction:[UIAlertActionactionWithTitle:@"消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) {
NSLog(@"点击了取消按钮");
UIAlertController
我们看到,它看起来正常工作了,需要注意的是,action事件的处理在添加是采用了Block方式添加进去,个人觉得这样更加出色。当在同一个类中创建多个UIAlertController对象时,还要像之前一样在代理种区分具体对象,或者采用runtime发发为每一个UIAlertController对象,通过下面的方法objc_setAssociatedObject 以及 objc_getAssociatedObject添加自己的具体操作,触发时,取出具体操作。另外在添加了按钮后,那么如何添加UITextField文本框。UIAlertController是采用[alertaddTextFieldWithConfigurationHandler:^(UITextField*textField) { textField.textColor= [UIColorredColor];textField.text=@"iOS8";[textFieldaddTarget:selfaction:@selector(usernameDidChange:)forControlEvents:UIControlEventEditingChanged];}];这样,就可以添加输入文本框了,就像这样
UIAlertController
从上面的代码,我在添加时,还对textField进行了一些设置,比如默认文字以及textColor。并且对textField添加了值改变的监听事件。各种处理,我们都可以脑洞大开。在一开始,说了基本可以认为UIAlertController == UIAlertView + UIActionSheet 上面演示的都是Alert,那么ActionSheet的方式呢,如下,只需要在创建代码UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"我是最牛逼的发"preferredStyle:UIAlertControllerStyleActionSheet];创建时将UIAlertControllerStyleAlert
改为UIAlertControllerStyleActionSheet 。alert就可以在底部弹出了,效果各位可以自己运行一下看看。需要注意的是:在UIAlertControllerStyleActionSheet样式下,是不可以添加UITextField。否则就会报错。4.简单的区别
1.UIAlertView
iOS系统为了保证UIAlertView在所有界面之上,它会临时创建一个新的UIWindow,通过将UIWindow的UIWindowLevel设置的更高,让UIAlertView盖在所有应用的界面之上
2.UIAlertController
是继承自UIViewController,它采用一个UIPopoverPresentationController类进行管理,UIPopoverPresentationController又继承自 UIPresentationController,其中的presentingViewController属性表示展示之前的Controller,presentedViewController属性表示被展示的Controller。另外这种方式,也统一了iPhone和iPad的使用方式Pages: 1/2
主题 : UIAlertAction 改变字体颜色问题 麻烦会的朋友看一下这个错误
级别: 骑士
UID: 400498
可可豆: 533 CB
威望: 526 点
在线时间: 1935(时)
发自: Web Page
来源于&&分类
UIAlertAction 改变字体颜色问题 麻烦会的朋友看一下这个错误&&&
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@&取消& style:UIAlertActionStyleCancel handler:nil];&&&&[cancelAction setValue:[UIColor redColor] forKey:@&_titleTextColor&];无法改变字体颜色&&错误信息报 键值不对 reason: '[&UIAlertAction 0x& setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key _titleTextColor.'
级别: 圣骑士
UID: 595660
可可豆: 1330 CB
威望: 1006 点
在线时间: 1094(时)
发自: Web Page
回 楼主(november11) 的帖子
貌似没问题啊
图片:QQ@2x.png
级别: 骑士
UID: 400498
可可豆: 533 CB
威望: 526 点
在线时间: 1935(时)
发自: Web Page
回 1楼(雪夜风暴) 的帖子
怎么会啊 不加这句代码 不报错 加了就报错 啥情况 xcode8.1&&
级别: 圣骑士
UID: 595660
可可豆: 1330 CB
威望: 1006 点
在线时间: 1094(时)
发自: Web Page
回 2楼(november11) 的帖子
我也是8.1 啊&&结果都截图给你看了
级别: 圣骑士
UID: 595660
可可豆: 1330 CB
威望: 1006 点
在线时间: 1094(时)
发自: Web Page
回 2楼(november11) 的帖子
新建个工程单独试试这个
级别: 骑士
UID: 400498
可可豆: 533 CB
威望: 526 点
在线时间: 1935(时)
发自: Web Page
回 3楼(雪夜风暴) 的帖子
有没有这么神奇&&奇了怪了
级别: 骑士
UID: 400498
可可豆: 533 CB
威望: 526 点
在线时间: 1935(时)
发自: Web Page
回 4楼(雪夜风暴) 的帖子
新建工程 也是没问题的 看来是其他错误了
级别: 圣骑士
UID: 595660
可可豆: 1330 CB
威望: 1006 点
在线时间: 1094(时)
发自: Web Page
回 6楼(november11) 的帖子
那你自己慢慢看咯
级别: 骑士
UID: 400498
可可豆: 533 CB
威望: 526 点
在线时间: 1935(时)
发自: Web Page
回 7楼(雪夜风暴) 的帖子
模拟器是什么版本的 麻烦你试下8.1版本的模拟器看看 我8.1的真机就奔溃
级别: 圣骑士
UID: 595660
可可豆: 1330 CB
威望: 1006 点
在线时间: 1094(时)
发自: Web Page
回 8楼(november11) 的帖子
&&UIActionSheet低版本应该用这个吧&&
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版(iOS开发)关于IOS9以后UIAlertView被UIAlerTController取代的有关问题 - wf的博客 - CSDN博客
(iOS开发)关于IOS9以后UIAlertView被UIAlerTController取代的有关问题
& IOS 9 以后 UIAlertView就不被苹果所推荐使用了,因此苹果推出了 UIAlerTController来取代UIAlertView,下面介绍下UIAlerTController的使用方式。
Tips:这个UIAlertController需要版本限制IOS8.0以上才可以使用
UIAlertController* alert=[UIAlertControlleralertControllerWithTitle:nilmessage:@&提示& preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertActionactionWithTitle:@&确定& style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction)
//这里是点击确定按钮以后需要实现的代码
[alert addAction:[UIAlertActionactionWithTitle:@&取消& style:UIAlertActionStyleCancelhandler:^(UIAlertAction* _Nonnullaction)
//这里是点击取消按钮以后需要实现的代码
[alert addAction:[UIAlertActionactionWithTitle:@&警告&style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*_Nonnullaction)
//这里是点击取消按钮以后需要实现的代码
[selfpresentViewController: alert animated:YEScompletion:nil];
//必要时需要返回主程序调用方法
dispatch_sync(dispatch_get_main_queue(), ^(){
// 这里的代码会在主线程执行
[self presentViewController:_alertViewErroranimated:YES completion:nil];
我的热门文章

我要回帖

更多关于 d触发器的功能 的文章

 

随机推荐