[英]2 or more viewcontrollers with same swift class crash
Hello I have multiple veiwcontorllers in my storyboard and when they have the same class (for exmaple viewocntorller.swift) and code like changing background color they crash with 您好,我的情节提要中有多个veiwcontorllers,并且当它们具有相同的类时(例如,viewocntorller.swift),并且代码(例如更改背景色)会崩溃
EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
Is there some way to fix this without using separate .swift files for each viewcontroller 是否有某种方法可以解决此问题,而无需为每个ViewController使用单独的.swift文件
Here are screenshot 这是截图
enter image description here 在此处输入图片说明
enter image description here 在此处输入图片说明
Has you can see the viewcontroller are in same class 你能看到viewcontroller在同一个类中吗
it's not the error of viewController. 这不是viewController的错误。 It means that your button reference is not there.
这意味着您的按钮引用不存在。 It's nil .
没关系 。 Verify that it's properly created.
验证它是否正确创建。
May be you've button outlet of one xib and not the other. 可能是您有一个xib的按钮插座,而没有另一个。 and when for your xib (with no button), viewDidLoad is called, button is nil.
当您的xib(无按钮)被调用时,viewDidLoad被调用,按钮为nil。 If you have buttons in both, then properly attach outlet.
如果两个按钮都有,请正确连接插座。 otherwise, make this button optional
否则,将此按钮设为可选
You don't have a button on your second view controller in your storyboard. 故事板中的第二个视图控制器上没有按钮。 Since you have an outlet to that button (the one on the first view controller) inside your code (linked to both viewcontrollers) and you specify a color for it your code will crash, because there is no button, calling a nil reference = crash.
由于您的代码中有一个按钮的出口(第一个视图控制器上的那个)(链接到两个视图控制器),并且您为其指定了一种颜色,因此代码将崩溃,因为没有按钮,因此调用nil reference = crash 。
To solve this you can add a button on the second view. 为了解决这个问题,您可以在第二个视图上添加一个按钮。
An outlet is not a basic optional, you are in fact telling the compiler that this button will NEVER be nil, that is what we call an Implicitly Unwrapped Optional. 插座不是基本的可选,您实际上是在告诉编译器该按钮永远不会为零,这就是我们所谓的“隐式展开的可选”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.