ios8之后的UIAlertController

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

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

IOS8之前

创建一个UIActionSheet

            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;

-(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

- (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之后,转而使用UIAlertController与preferredstyle的UIAlertControllerstyleactionsheet。

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

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


            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];

或者你可以
    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.");
    }];
[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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值