美文网首页iOS技术
使用KVC修改UIAlertAction颜色遇到的一个的问题

使用KVC修改UIAlertAction颜色遇到的一个的问题

作者: 天空中的球 | 来源:发表于2016-08-06 15:53 被阅读1588次

缘由:之前在使用UIAlertAction的时候想动态的修改它的颜色,便直接通过KVC来设置,但是后来和简友沟通中发现在iOS 8.3以下这样是行不通的,然后自己测试了一下在iOS 8.1的情况,发现其_titleTextColor私有属性是不存在的。

用KVC的方法。

 [doneAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];

先PS一下,可满足iOS 8.3 以下的解决办法。

alertController.view.tintColor = [UIColor yellowColor];

再 补充一下,在iOS 8.4 上,由于View的层次原因,必须将其写在Present 之后

[self presentViewController:alertController animated:YES completion:nil];
//修改按钮颜色 此处一定要写在后面,否则在8.4会有问题
alertController.view.tintColor =  [UIColor yellowColor];
简单分析

先通过runtime获取UIAlertAction的属性列表和成员变量列表:

unsigned int count;// 记录属性个数
objc_property_t *properties = class_copyPropertyList([UIAlertAction class], &count);
// 遍历
NSMutableArray *propertiesArray = [NSMutableArray array];
for (int i = 0; i < count; i++) {
    // objc_property_t 属性类型
    objc_property_t property = properties[i];
    // 获取属性的名称 C语言字符串
    const char *cName = property_getName(property);
    // 转换为Objective C 字符串
    NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
    [propertiesArray addObject:name];
}
free(properties);
NSLog(@"propertiesArray == %@",propertiesArray);

 //获取成员变量列表
NSMutableArray *ivarArray = [NSMutableArray array];
Ivar *ivarList = class_copyIvarList([UIAlertAction class], &count);
for (int i = 0; i < count; i++) {
    Ivar myIvar = ivarList[i];
    const char *ivarName = ivar_getName(myIvar);
    [ivarArray addObject:[NSString stringWithUTF8String:ivarName]];
}
free(ivarList);
NSLog(@"ivarArray == %@",ivarArray);

iOS 9.1的打印


propertiesArray == (
    title,
    style,
    enabled,
    handler,
    simpleHandler,
    image,
    shouldDismissHandler,
    "_descriptiveText",
    contentViewController,
    keyCommandInput,
    keyCommandModifierFlags,
    "_representer",
    "_isPreferred",
    "_alertController"
)

ivarArray == (
    "_title",
    "_titleTextAlignment",
    "_enabled",
    "_checked",
    "_isPreferred",
    "_imageTintColor",
    "_titleTextColor",
    "_style",
    "_handler",
    "_simpleHandler",
    "_image",
    "_shouldDismissHandler",
    "__descriptiveText",
    "_contentViewController",
    "_keyCommandInput",
    "_keyCommandModifierFlags",
    "__representer",
    "__alertController"
)

iOS 8.1的打印

propertiesArray == (
    title,
    style,
    enabled,
    handler,
    simpleHandler,
    image,
    shouldDismissHandler,
    "_descriptiveText",
    "_representer",
    "_isDefault",
    "_alertController"
)

ivarArray == (
    "_title",
    "_enabled",
    "_checked",
    "__isDefault",
    "_style",
    "_handler",
    "_simpleHandler",
    "_image",
    "_shouldDismissHandler",
    "__descriptiveText",
    "__representer",
    "__alertController"
)

可以看到_titleTextColor是其成员变量的, 而且在iOS 8.1的时候是没有的。所以此处使用KVC,是有问题的,也说明使用KVC要谨慎啊

其实,我想当我们有时使用KVC 传入一个不确定的key 进入,犹如在用 stroyboard 的时候,连线后而又忘记删除某个key一样的

this class is not key value coding-compliant for the key "someKey"

但是又想用怎么办呢?

  • 首先通过runtime确认这个Key是否存在?
  • 然而针对不同版本之间可能不存在的情况,需要一一验证

针对不同版本的问题,暂时没有很好的验证方法——验证这个key 是否是有效的? 如果哪位朋友有好的方法验证key的有效性,欢迎告之。

总结:使用KVC要谨慎啊!!!

相关文章

网友评论

    本文标题:使用KVC修改UIAlertAction颜色遇到的一个的问题

    本文链接:https://www.haomeiwen.com/subject/ucbcsttx.html