【问题标题】:Swift: unable to access subviews from xibSwift:无法从 xib 访问子视图
【发布时间】:2015-06-13 04:15:13
【问题描述】:

我从 xib 创建了一个自定义 uibutton,并在 viewController xib 文件中使用了这个 customButton。这个自定义视图有一个名为 buttonLabel 的子视图 UILabel。

我已经正确设置了 customButton xib 的类,并在 viewController 中为它设置了出口。当 viewController 中的 viewDidLoad 时,我试图像这样访问 self.customButton.buttonLabel:

self.customButton.buttonLabel.text = "something"

结果是运行时崩溃并显示以下消息:

致命错误:在展开可选值时意外发现 nil

原因是当时self.customButton.buttonLabel是nil。我已经确保 xib 文件也与 swift 类名具有相同的名称。可能是什么问题?

提前致谢

【问题讨论】:

  • 您可能会在加载 xib 之前使用此 cutomButton。
  • 我已经尝试在 viewDidLoad、viewWillAppear 和 viewWillLayoutSubviews 中访问 c​​ustomButton。但在所有情况下,customButton 都存在,但没有为 customButton 加载 xib,因此 customButton 的子视图为 nil。

标签: ios xcode swift


【解决方案1】:

删除 xib 文件并重新创建它解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多