简体   繁体   English

2个或更多具有相同swift类崩溃的视图控制器

[英]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.

相关问题 2个或更多具有相同类的UIViewcontroller崩溃 - 2 or more UIViewcontroller with the same class crash 在 Swift 中的 2 个以上视图控制器之间传递数据 - Passing data between more than 2 viewcontrollers in Swift 迅捷2在不同的视图控制器中应用相同的背景 - swift 2 Applying the same background in different viewcontrollers Swift3在viewControllers之间共享了一个类实例 - Swift3 shared a class instances between viewControllers 使用多个ViewController初始化类问题(Swift) - Initializing class issue using multiple ViewControllers (Swift) 自定义UIButton类,对不同的ViewControllers具有相同的操作 - Custom UIButton class with same action for different ViewControllers 如何在 ViewControllers 之间给不同的 tableViews 相同的动作 [swift] - How to give different tableViews same action between ViewControllers [swift] 迅捷类和目标-c ViewController之间的NotificationCenter网络 - NotificationCenter networking between swift class and objective -c Viewcontrollers SpriteKit Swift:当bodyA同时与多个bodyB碰撞时,如何防止崩溃? - SpriteKit Swift: How to prevent a Crash when bodyA collisions with more than one bodyB at the same time? 从共享相同自定义单元类的ViewController自定义TableView单元? - Customize TableView Cells from ViewControllers sharing the same Custom Cell Class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM