【问题标题】:respondsToSelector - not working响应选择器 - 不工作
【发布时间】:2023-03-20 21:33:01
【问题描述】:

我已经阅读了大约 10 篇文章,但还没有发现我的实现有什么问题。

这个应用程序是用 iOS 6 编写的,但更新到 iOS7,所以我想同时支持 iOS6 和 iOS7。但是,如果我在 iOS6 设备上运行仅限 iOS7 的方法,它就会中断。 所以我想添加responsToSelector,检查它是否有iOS7,但由于某种原因,if总是返回false。

AppDelegate.m:

if ([[UINavigationBar appearance] respondsToSelector:@selector(shadowImage)])

if ([[UINavigationBar appearance] respondsToSelector:@selector(setShadowImage:)])

谁能告诉我我做错了什么?

编辑:我尝试将部署目标设置为 iOS6 和 iOS7,两种情况都返回 false。

Edit2:如果我删除 if 语句并调用该方法,它将在 iOS7 中按预期工作。

【问题讨论】:

    标签: objective-c ios6 uinavigationbar ios7 respondstoselector


    【解决方案1】:

    而不是查询 [UINavigationBar appearance] 的选择器,只是

    [UINavigationBar instancesRespondToSelector:@selector(shadowImage)]
    

    另一种解决方案可能是检查 iOS 版本

    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
        NSLog(@"iOS >= 7.0");
    

    【讨论】:

    • 它有效,你能解释一下为什么我需要使用 instacesRespondToSelector 而不是 respondsToSelector 吗?我真的很想知道我为什么要使用某些东西:)
    • 我更喜欢响应选择器的方法,更动态。
    • 这是因为appearance代理方法。您可以找到更多信息there
    • 也感谢您向我展示systemVersion方法,我需要它来根据它设置UINavigationBar大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2011-10-13
    • 1970-01-01
    • 2015-06-21
    • 2019-07-11
    相关资源
    最近更新 更多