【发布时间】: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 中访问 customButton。但在所有情况下,customButton 都存在,但没有为 customButton 加载 xib,因此 customButton 的子视图为 nil。