20

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"); 

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?

4

1 回答 1

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...");
于 2012-09-19T23:42:57.100 回答