将viewcontroller分配给prepareForSegue中的变量时,Swift EXC_BREAKPOINT

Sim*_*and 11 ios swift

我尝试使用destinationViewController执行变量赋值时出错.

错误消息是这样的:线程1:EXC_BREAKPOINT(代码= 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

    }
}
Run Code Online (Sandbox Code Playgroud)

在另一个文件中,它看起来像这样.

var email: String?
Run Code Online (Sandbox Code Playgroud)

在顶部.然后这个:

override func viewDidLoad() {
    super.viewDidLoad()

    println("Email is:")
    println(email)
    println("Email was")
}
Run Code Online (Sandbox Code Playgroud)

但我从来没有进入第二档.

它是将vc = segue.destinationViewController作为标记为错误的LoggedInViewController的行.

两个swift文件都连接到导航控制器.

我不知道你还需要什么,但我当然会发布你需要了解的所有代码!

谢谢!

Yat*_*B L 28

在你的情况下目标控制器是导航控制器而不是你的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

    }
}
Run Code Online (Sandbox Code Playgroud)