respondsToSelector - 不工作

时间:2013-09-23 07:20:05

标签: objective-c ios6 uinavigationbar ios7 respondstoselector

我已经阅读了10篇帖子,但没有发现我的实施有什么问题。

此应用程序是用iOS 6编写的,但已更新到iOS7,所以我想为iOS6和iOS7提供支持。但是,如果我在iOS6设备上运行仅支持iOS7的方法,它就会中断。 所以我想添加respondsToSelector,检查它上面有iOS7,但由于某种原因,if总是返回false。

AppDelegate.m:

if ([[UINavigationBar appearance] respondsToSelector:@selector(shadowImage)])

if ([[UINavigationBar appearance] respondsToSelector:@selector(setShadowImage:)])

有人能告诉我我做错了什么吗?

编辑:我尝试将部署目标设置为iOS6和iOS7,两种情况都返回false。

Edit2:如果删除if语句并调用该方法,它将按照iOS7中的预期工作。

1 个答案:

答案 0 :(得分:2)

不是为选择器查询[UINavigationBar appearance],而是

[UINavigationBar instancesRespondToSelector:@selector(shadowImage)]

另一种解决方案可能是检查iOS版本

if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
    NSLog(@"iOS >= 7.0");
相关问题