本文介绍了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因外观代理失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:09