2015-06-13 121 views
1

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

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

self.customButton.buttonLabel.text = "something" 

结果是运行时崩溃,出现以下消息:

致命错误:意外发现零而展开的可选值

原因是self.customButton.buttonLabel当时为零。我确定xib文件与swift类名同名。可能是什么问题?

在此先感谢

+0

您可能在xib加载之前使用这个cutomButton。 –

+0

我试着在viewDidLoad,viewWillAppear和viewWillLayoutSubviews中访问customButton。但在所有情况下,customButton都在那里,但xib没有为customButton加载,所以customButton的子视图为零。 –

回答

0

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