Swift:将数据传递给 NavigationController 的第一个子节点,使用 storyboardID 实例化

标签 swift

我想将数据传递给嵌入在 navigationController 中的第一个 viewController

要访问这个导航 Controller ,它有一个 storyBoardID,我到达实例化 navigationController 但我不能向他传递数据,

这是我的代码:

extension UINavigationController {

func dismissAndPresentNavigationController(from storyboard: UIStoryboard?, identifier: String) {
    guard let navigationController = storyboard?.instantiateViewController(withIdentifier: identifier) as? UINavigationController else { return }

    print("OK")
    if let nav = navigationController.navigationController?.viewControllers.first as? ChatBotViewController{
        print("OK2")
    }

    self.dismiss(animated: false, completion: nil)
    self.present(navigationController, animated: true, completion: nil)
}

我在参数中输入的标识符是导航 Controller 的storyBoardID

如何向navigationcontroller的第一个 Controller 传输数据?

解决方案:

extension UINavigationController {

func dismissAndPresentNavigationController(from storyboard: UIStoryboard?, identifier: String, with fittoBottle: FittoBottle) {
    guard let navigationController = storyboard?.instantiateViewController(withIdentifier: identifier) as? UINavigationController else { return }

    if let nav = navigationController.viewControllers.first as? ChatBotViewController{
        nav.fittoBottle = fittoBottle
    }

    self.dismiss(animated: false, completion: nil)
    self.present(navigationController, animated: true, completion: nil)
}

最佳答案

从 Storyboard实例化导航 Controller 后,您将能够通过 navigationController.viewControllers.first 访问 Root View Controller 。

guard let navigationController = storyboard?.instantiateViewController(withIdentifier: identifier) as? UINavigationController else { return }

if let chatBotViewController = navigationController.viewControllers.first as? ChatBotViewController {
    chatBotViewController.fittoBottle = fittoBottle
}

self.dismiss(animated: false, completion: nil)
self.present(navigationController, animated: true, completion: nil)

关于Swift:将数据传递给 NavigationController 的第一个子节点,使用 storyboardID 实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169595/

相关文章:

ios - 创建自定义核心数据类以子类化 MKAnnotation?

swift - 只能返回,不能赋值,Self?

ios - Swift - 如何显示跟踪事件的 MKPolyline

ios - 在手机休眠时运行计时器

xcode - 将字典传递给 watch

ios - 为什么只更新第一个单元格中的图像,而 uicollectionview 中的其余单元格未更新。 ios swift

ios - Xcode 6 中与 iPhone 5/6/6+ 屏幕分辨率相关的相同按钮位置

ios - 使用 Swift 格式化段落文本

swift - 为什么 CLGeoCoder 无法获取任何内容?

iOS swift : Maintaining Toggle Button State In A Cell