我在视图控制器上有一个按钮 - 该视图是类“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