ios8之后的UIAlertController详解

来源:互联网 发布:论文改数据会被发现吗 编辑:程序博客网 时间:2024/05/12 17:58

昨天工程里需要将 UIActionSheet 中字体的颜色统一为系统的蓝色,所以便去网上找办法。

办法很简单,遍历出UIActionSheet中的子控件。当然 这是ios8之前的方法,为什么这么说呢,等下你就知道了,先说说ios8之前的调用吧。

IOS8之前

创建一个UIActionSheet

[objc] view plain copy
print?
  1. UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:@”拨号” otherButtonTitles:@”发送短信”@”复制名片”, nil nil];  
  2. t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;  
  3. [t_actionSheet showInView:self.view];  
            UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拨号" otherButtonTitles:@"发送短信", @"复制名片", nil];            t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;            [t_actionSheet showInView:self.view];


// Called when a button is clicked. The view will be automatically dismissed after this call returns

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

[objc] view plain copy
print?
  1. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  2. {  
  3.     if (buttonIndex == 0) {  
  4.         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@”tel://%@”,_mar_num[_selectRow][1]]]];  
  5.     }else if (buttonIndex == 1) {  
  6.         [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@”sms://%@”,_mar_num[_selectRow][1]]]];  
  7.     }else if(buttonIndex == 2) {  
  8.         UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];  
  9.         pasteboard.string = [NSString stringWithFormat:@”%@:%@”,_mar_num[_selectRow][2],_mar_num[_selectRow][1]];  
  10.     }  
  11. }  
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 0) {        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mar_num[_selectRow][1]]]];    }else if (buttonIndex == 1) {        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",_mar_num[_selectRow][1]]]];    }else if(buttonIndex == 2) {        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];        pasteboard.string = [NSString stringWithFormat:@"%@:%@",_mar_num[_selectRow][2],_mar_num[_selectRow][1]];    }}


- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;  // before animation and showing view

[objc] view plain copy
print?
  1. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet  
  2. {  
  3.     for (id subViwe in actionSheet.subviews) {  
  4.         if ([subViwe isKindOfClass:[UIButton class]]) {  
  5.             UIButton *button = (UIButton*)subViwe;  
  6.             [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal];  
  7.         }  
  8.     }  
  9. }  
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{    for (id subViwe in actionSheet.subviews) {        if ([subViwe isKindOfClass:[UIButton class]]) {            UIButton *button = (UIButton*)subViwe;            [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal];        }    }}

赋上断点后的截图,


你可以清晰的看到,在即将出现的UIActionSheet 里面是有5个子视图的,其中4个按钮都为我自己添加,(第0个可能是背景颜色吧、没有去试过)然后你可以看代码,取出之后赋给一个 UIButton 再改变属性。

当你这样相等之后,两个按钮其实占有的是同一个指针,所以可以改变其中按钮的属性。当然你也可以直接用subView(UIAlertButton)改变属性。


这样之后,便可以改变字体中颜色了。

IOS8之后

在IOS8之后UIActionSheet变成了UIAlertController,官方是这样说的

一个UIAlertController对象向用户显示一个警告信息。这类取代UIActionSheetUIAlertView类显示警报。行动和你想要的风格配置警报控制器后目前使用的presentviewcontroller动画完成:方法
(现在已经是一个控制器了 区别于以前 现在继承于 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
以前NS_CLASS_AVAILABLE_IOS(2_0) @interface UIActionSheet : UIView 
ios8之后 你需要这样创建,当然,你用以前的方法创建的在ios8上同样可以显示,点击等等,只不过有些方法就没用了,例如本文中修改字体的方法,因为不再是 UIView 而是 UIViewController ,而且要注意一点的是,当你用下述 ios8之后创建的方法,跑在ios8的机子上,代理是不会再执行的,不知道对不对,不过我跑的时候 上面两个代理方法就没有执行。。。

重要:UIActionSheet在iOS 8不赞成。注意,UIActionSheetdelegate也是过时的来创建和管理动作片iOS 8之后转而使用UIAlertControllerpreferredstyleUIAlertControllerstyleactionsheet

在应用程序的目标版本的iOS到iOS 8之前,使用UIActionSheet类向用户显示一组选择如何进行一个给定的任务。你也可以使用动作片提示用户确认潜在危险的行动。动作包含一个可选的标题和一个或多个按钮每一个对应于所采取的行动

使用该类的属性和方法来配置动作片的消息,风格,和之前提交按钮。你也应该指定一个代表你的动作片。你的委托对象负责执行与任何按钮时,他们被窃听和应符合uiactionsheetdelegate协议的行动。有关实现委托的方法的更多信息UIActionSheetdelegate协议参考


[objc] view plain copy
print?
  1. UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];  
  2. [alertCtrl addAction:[UIAlertAction actionWithTitle:@”取消”  
  3.                                               style:UIAlertActionStyleCancel  
  4.                                             handler:^(UIAlertAction *action) {  
  5.                                                 //code  
  6.                                             }]];  
            UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];            [alertCtrl addAction:[UIAlertAction actionWithTitle:@"取消"                                                          style:UIAlertActionStyleCancel                                                        handler:^(UIAlertAction *action) {                                                            //code                                                        }]];

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

或者你可以
[objc] view plain copy
print?
  1. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];  
  2.   
  3. // Create the actions.  
  4. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {  
  5.     NSLog(@”The \”Okay/Cancel\” alert’s cancel action occured.”);  
  6. }];  
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];    // Create the actions.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");    }];
[objc] view plain copy
print?
  1. [alertController addAction:cancelAction];    
  2. [self presentViewController:alertController animated:YES completion:nil];  
[alertController addAction:cancelAction];  [self presentViewController:alertController animated:YES completion:nil];

其中,几个属性不用说你也应该知道

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {

    UIAlertControllerStyleActionSheet = 0,

    UIAlertControllerStyleAlert

NS_ENUM_AVAILABLE_IOS(8_0);


typedef NS_ENUM(NSInteger, UIAlertActionStyle) {

    UIAlertActionStyleDefault = 0,

    UIAlertActionStyleCancel,

    UIAlertActionStyleDestructive

NS_ENUM_AVAILABLE_IOS(8_0);


在这里,如何修改颜色字体呢 我想可能是  UIAlertController 的这个方法吧。。

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

在这里 我直接用了UIAlertActionStyleDestructive 把其中的某项改成UIAlert的红色,所以没有真正自定义颜色,不过去了解一下估计就可以了,还是挺简单的。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二年级成绩很差怎么办 班级差生应该怎么办 班级差生太多怎么办 初三孩子英语差怎么办 初三英语基础差怎么办 学生学习效率低怎么办 摔伤后皮下血肿怎么办 孩子学习效率低怎么办 很多作业没写完怎么办? 小学孩子成绩差怎么办 孩子脚上起水泡怎么办 小学一年级体育课哑铃怎么办 托福成绩失效后怎么办 去外国旅游不适应怎么办 一年级语文成绩差怎么办 一年级小孩成绩差怎么办 一年级小孩基础差怎么办 一年级小孩语文差怎么办 一年级小孩数学差怎么办 一年级小孩理解能力差怎么办 一年级小孩作文差怎么办 一年级小孩记忆力差怎么办 孩子的记忆力不好怎么办 孩子记忆力不好怎么办呢 小学三年级学生不听话怎么办 司法考试成绩单没打印怎么办 高一200分怎么办 初一孩子语文差怎么办 大学专业没选好怎么办 减肥口渴的厉害怎么办 开胸手术后胸闷气短怎么办 打游戏上网成瘾怎么办 老公打游戏成瘾怎么办 我老是沉迷游戏怎么办 18孩子沉迷游戏怎么办 志愿提交码丢失怎么办 幼儿园报名没进怎么办 考完教师资格证怎么办 两套房改房怎么办 cad打开无响应怎么办 高中生脸上痘痘怎么办