respondsToSelector 外观代理失败

时间:2023-03-22
本文介绍了respondsToSelector 外观代理失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试通过在 [UIBarButtonItem 外观] 上运行 respondsToSelector 来检测 iOS 6 特定的外观方法.但是,无论我指定什么选择器,它总是为我返回 NO:

I’m trying to detect an iOS 6-specific appearance method, by running respondsToSelector on the [UIBarButtonItem appearance]. However, it always returns NO for me, whatever selector I specify:

// Should show NOPE in iOS 5, YEP in iOS 6. Shows NOPE always
NSLog(@"%@", [[UIBarButtonItem appearance] respondsToSelector:@selector(setBackgroundImage:forState:style:barMetrics:)] ? @"YEP" : @"NOPE"); 

// Should show YEP in both iOS 5 and iOS 6. Shows NOPE always
NSLog(@"%@", [[UIBarButtonItem appearance] respondsToSelector:@selector(setBackgroundImage:forState:barMetrics:)] ? @"YEP" : @"NOPE"); 

实际上,在它们各自的 iOS 版本上使用这些方法都可以正常工作,但我似乎无法检测到哪一个对我可用.那么我该如何正确地做到这一点呢?

Actually using those methods works fine on their respective versions of iOS, but I can’t seem to detect which one is available to me. So how do I properly do that?

推荐答案

不要检查外观代理.你永远不能依赖它,因为它是一个代理.相反,直接检查具有新方法的项目,在这种情况下,UIBarButtonItem:

Don't check the appearance proxy. You can never rely on that, since it's a proxy. Instead, directly check the item that has the new method, in this case, the UIBarButtonItem:

BOOL hasNewMethod = [UIBarButtonItem instancesRespondToSelector:@selector(setBackgroundImage:forState:style:barMetrics:)];
if( hasNewMethod )
  NSLog(@"Running iOS 6 with new method");
else
  NSLog(@"Current OS doesn't support method...");

这篇关于respondsToSelector 外观代理失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:需要通过 iOS SDK 在我的服务器上使用 JSON 对用户进行身份验证 下一篇:iOS5:在模态 UIViewController 中播放 Youtube 视频时 UIWebView 出现异常

相关文章