gpt4 book ai didi

objective-c - respondsToSelector 外观代理失败

转载 作者:太空狗 更新时间:2023-10-30 03:13:35 26 4
gpt4 key购买 nike

我试图通过在 [UIBarButtonItem appearance] 上运行 respondsToSelector 来检测特定于 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 版本上效果很好,但我似乎无法检测到我可以使用哪个方法。那么我该如何正确地做到这一点呢?

最佳答案

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

关于objective-c - respondsToSelector 外观代理失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12504304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com