respondsToSelector因外观代理而失败

Arn*_*old 20 xcode objective-c ios5 uiappearance ios6

我想检测的iOS 6特定外观的方法,通过运行respondsToSelector[UIBarButtonItem appearance].但是,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"); 
Run Code Online (Sandbox Code Playgroud)

实际上使用这些方法在他们各自的iOS版本上工作正常,但我似乎无法检测到哪一个可供我使用.那我该怎么做呢?

Jas*_*oco 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...");
Run Code Online (Sandbox Code Playgroud)