【问题标题】:ios - Error when hiding button through code Objective-cios - 通过代码Objective-c隐藏按钮时出错
【发布时间】:2017-07-07 15:18:35
【问题描述】:

我试图通过代码隐藏一个按钮,但每次我试图隐藏它时程序都会崩溃。

我得到的错误:

'NSInvalidArgumentException', reason: '-[UIBarButtonItem setHidden:]: unrecognized selector sent to instance 0x14ef8f30'

.h 文件代码:

@property (strong) UIButton *takeAll;

.m 代码:​​

    @synthesize takeAll;

// function

   [self.takeAll setHidden:YES];

【问题讨论】:

  • 添加takeAll按钮分配代码
  • takeAll 是 UIBarButtonItem,您可能希望将插座与此 buttonItem 内的按钮相关联。
  • 由于某种原因,当我将其声明为 UIBarButtonItem 时无法隐藏它,我该如何解决?
  • 为什么有@synthesize 行?很多年都不需要了。您应该找到最新的 Objective-C 教程。
  • iOS 新手,正在处理一个较旧的项目,所以我一直在遵循格式。感谢您的建议

标签: ios objective-c uibutton


【解决方案1】:

错误清楚地表明 '您正在尝试设置 UIBarButtonItem 的隐藏值

您创建了一个UIButton 对象并分配为UIBarButtonItem。这是错误的。应该是

self.takeAll = [UIButton buttonWithType:UIButtonTypeCustom]

如果你需要UIBarButtonItem 那么

@property (strong) UIBarButtonItem *takeAll;

如果你想隐藏UIBarButtonItem

 self.takeAll.enabled = false
 self.takeAll.tintColor = UIColor.clearColor

启用条形按钮项

 self.takeAll.enabled = true
 self.takeAll.tintColor = UIColor.blueColor

【讨论】:

  • 是的,抱歉没有考虑改变颜色来隐藏它。谢谢
【解决方案2】:

UIBarButtonItem 没有setHidden: 选择器。您可能希望将按钮的色调设置为清晰的颜色并将其禁用,这将提供相同的功能。

【讨论】:

    【解决方案3】:

    如果你在 UIBarButtonItem 中有 UIView 的子类,你可以用这段代码隐藏它`

    [barButtonItem.customView setHidden:YES];

    【讨论】:

      【解决方案4】:

      显示的崩溃在 UIBarButtonItem 中。 在接口文件中,声明用于 UIButton。可能是您正在尝试将 UIButton 链接到 UIBarButtonItem。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 2015-04-01
        • 1970-01-01
        • 2018-01-01
        • 2016-02-07
        相关资源
        最近更新 更多