2015-06-13 24 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文件並重新創建它解決了問題。