我想将数据传递给嵌入在 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/