本文介绍了respondsToSelector - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read through like 10 posts, but haven't found what's wrong with my implementation.

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

This app was written in iOS 6 but updated to iOS7, so I want to offer support for both iOS6 and iOS7. But if I run an iOS7-only method on an iOS6 device, it breaks. So I thought of adding respondsToSelector, to check it has iOS7 on it, but for some reason, the if always returns false.

AppDelegate.m:

AppDelegate.m:

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

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

有人可以告诉我我是做什么的做错了?

Can someone tell me what am I doing wrong?

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

I've tried with deployment target set to both iOS6 and iOS7, both cases return false.

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

If I remove the if statement and call the method, it works as intended in iOS7.

推荐答案

选择器查询 [UINavigationBar外观] ,只需

[UINavigationBar instancesRespondToSelector:@selector(shadowImage)]

替代解决方案可能是检查iOS版本

an alternative solution may be checking the iOS version

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

这篇关于respondsToSelector - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:40