ios DestinationViewController Segue 和 UINavigationController swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28573635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:52:04  来源:igfitidea点击:

DestinationViewController Segue and UINavigationController swift

iosswiftsegue

提问by YichenBman

So I have a prepareForSegue method like this:

所以我有一个像这样的 prepareForSegue 方法:

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

        let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController

        addEventViewController.newTagArray = newTagArray

    }
}

However, at runtime the app crashes and logs spit out this message:

但是,在运行时,应用程序崩溃并在日志中输出以下消息:

Could not cast value of type 'UINavigationController'

无法转换“UINavigationController”类型的值

The error comes because the addEventController is embedded in a navigation controller, however I'm not sure how I would set up the segue so that the destinationViewController is set to the NavigationController, but also allow me to pass variables in the prepareForSegue method to the addEventController

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

So how would I do this using Swift?

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

I extremely appreciate your help and time you may offer in response. It helps alot

我非常感谢您提供的帮助和时间作为回应。它有很大帮助

回答by rdelmar

You only need to access the navigation controller's topViewController, which will be the AddEventViewController. Here is the code:

您只需要访问导航控制器的 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

    }
}