Swift EXC_BREAKPOINT将viewController分配给prepareForSegue中的variables
我尝试执行一个variables赋值与我的destinationViewController时得到一个错误。
错误信息是这样的:线程1:EXC_BREAKPOINT(code = EXC_I386_BPT,子码= 0x0)
这在我的prepareForSegue函数中。
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if segue.identifier == "LoginSegue"{ let vc = segue.destinationViewController as LoggedInViewController vc.email = emailTextfield.text } }
在另一个文件中,它看起来像这样。
var email: String?
这是在顶部。 那么这个:
override func viewDidLoad() { super.viewDidLoad() println("Email is:") println(email) println("Email was") }
但我从来没有进入第二个文件。
这是行vc = segue.destinationViewController作为LoggedInViewController标记为错误。
两个swift文件都连接到导航控制器。
我不知道你需要什么,但我当然会发布你需要了解的所有代码!
谢谢!
在你的情况目标控制器是导航控制器不是你的LoggedInViewController ,所以segue.destinationViewController as LoggedInViewController
是一个错误,因此它是崩溃。
像这样尝试
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if segue.identifier == "LoginSegue"{ let navigationController = segue.destinationViewController as UINavigationController let vc = navigationController.topViewController as LoggedInViewController vc.email = emailTextfield.text } }
如果有人来这里是因为这是EXC_BREAKPOINT的第一个命中:
对于我来说,这个非常明显的exception是因为一个fatal error: unexpectedly found nil while unwrapping an Optional value
而引发的fatal error: unexpectedly found nil while unwrapping an Optional value
,这是因为在初始化之前使用了IBOutlet。