問題描述
我正在嘗試通過在 [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 外觀代理失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!