【问题标题】:unrecognized selector sent to instance UIBarButtonItem无法识别的选择器发送到实例 UIBarButtonItem
【发布时间】:2012-11-08 15:03:42
【问题描述】:

我正在尝试自定义导航栏及其在我的 AppDelegate 文件中的按钮:

// first I am customizing the done button

    [[UIBarButtonItem appearance] setBackgroundImage:buttonDone forState:UIControlStateNormal style:UIBarButtonItemStyleDone barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackgroundImage:buttonDonePressed forState:UIControlStateHighlighted style:UIBarButtonItemStyleDone barMetrics:UIBarMetricsDefault];


//and then I am customizing the back button


    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBackPressed forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

我收到[_UIBarItemAppearance setBackgroundImage:]: unrecognized selector sent to instance 的完成按钮。奇怪的是,后退按钮的自定义有效,而完成按钮的自定义使应用程序崩溃。任何想法为什么?

*PSbuttonDone、buttonDonePressed、buttonBack 和 buttonBackPressed 是我创建的四个 UIImage-s*

更新:在 iOS 5.0 模拟器上,应用程序崩溃,但在 6.0 上它可以工作。关于如何避免 5.0 崩溃的任何建议?

【问题讨论】:

  • 你在开发什么 ios 版本?
  • 你意识到你正在使用不同的方法来返回按钮吗?
  • ...您运行的iOS版本是什么?
  • 在 iOS 6 上运行正常,在 iOS 5 上崩溃,有什么建议可以避免崩溃吗?

标签: ios uibarbuttonitem unrecognized-selector uibarbuttonitemstyle


【解决方案1】:

根据文档,此签名仅适用于 iOS 6.0+

您可以通过以下方式测试对象方法的可用性:

[obj respondsToSelector:@selector(<methodName>)]

那就试试吧:

id item = [UIBarButtonItem appearance];
if ([item respondsToSelector:@selector(setBackgroundImage:forState:style:barMetrics:)]) {
   // Use new signature
 } else {
   // Use old signature
 }

【讨论】:

  • 好的,你的意思是我不能自定义 iOS 5 上的按钮?
  • 由于我不熟悉定制,但我不能告诉你是否可以或不能。我的回答向您展示了如何识别这种情况。查看按钮项目的文档并尝试确定是否可能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多