一尘不染

DestinationViewController Segue和UINavigationController迅速

swift

所以我有这样的prepareForSegue方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "fromEventTableToAddEvent" {

        let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController

        addEventViewController.newTagArray = newTagArray

    }
}

但是,在运行时,应用程序崩溃并记录了以下消息:

无法转换类型为’UINavigationController’的值

出现错误是因为addEventController嵌入在导航控制器中,但是我不确定如何设置segue,以便将destinationViewController设置为NavigationController,但还允许我将prepareForSegue方法中的变量传递给addEventController。

那么我将如何使用Swift做到这一点?

非常感谢您的帮助和时间。它可以帮助很多


阅读 174

收藏
2020-07-07

共1个答案

一尘不染

您只需要访问导航控制器的topViewController,它将是AddEventViewController。这是代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "fromEventTableToAddEvent" {

        let nav = segue.destinationViewController as! UINavigationController
        let addEventViewController = nav.topViewController as! AddEventViewController

        addEventViewController.newTagArray = newTagArray

    }
}
2020-07-07