respondsToSelector因外观代理而失败

时间:2012-09-19 23:33:29

标签: objective-c xcode ios5 ios6 uiappearance

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

// 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版本上运行良好,但我似乎无法检测到哪些可供我使用。那我该怎么做呢?

1 个答案:

答案 0 :(得分:36)

不要检查外观代理。你永远不能依赖它,因为它是一个代理。而是直接检查具有新方法的项目,在本例中为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...");