我在視圖控制器上有一個按鈕 - 該視圖是類「MainView」包含另一個viewContainer,其視圖是類「SecondView」。更改不同類別按鈕的屬性
我有一些代碼在調用MainView.swift的函數secondView.swift文件MainView().updateButton()
中的MainView的updateButton
函數調用正確時,它僅包含print("called")
- 但是如果我改變功能button.backgroundColor = UIColor.blue
則與該錯誤在該行上崩潰:
fatal error: unexpectedly found nil while unwrapping an Optional value
我該如何阻止這次崩潰?
您可以使用NotificationCenter。這裏是一個教程https://medium.com/ios-geek-community/pass-data-with-nsnotification-in-swift-3-73743723c84b#.7k8kpqfd7。你將改變一個類的特性而不是根據數據傳遞例如。您可以根據需要更改代碼。 – theduman
@theduman - 這工作完美 - 把它作爲一個答案,並將其標記爲正確 – ZiEiTiA