Swift:将数据从视图控制器传递到导航控制器
Posted
技术标签:
【中文标题】Swift:将数据从视图控制器传递到导航控制器【英文标题】:Swift: Pass data from a view controller to a navigation controller 【发布时间】:2019-05-17 15:39:12 【问题描述】:我想将数据从一个视图控制器传递到另一个视图控制器,但我在它们之间有一个导航控制器,所以有一个“present”函数而不是“performSegue”。
private func IniciarMainAdmin()
let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else
return
present(mainAdminNVC, animated: true, completion: nil)
我已尝试使用此代码,但它对我不起作用:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let destViewController = segue.destination as! NCMainAdmin
let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
secondViewcontroller.adminUserData = "This is a test"
注意事项:
NCMainAdmin 是导航控制器 NCMenuAdmin 是 Navigation Controller 的第一个 View Controller谢谢!
【问题讨论】:
【参考方案1】:要触发您的代码prepare(for
,您需要创建从发送方 vc 到导航的 segue,所以要么
private func IniciarMainAdmin()
let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else
return
let first = mainAdminNVC.viewControllers.first as! NCMenuAdmin
first.adminUserData = "This is a test"
present(mainAdminNVC, animated: true, completion: nil)
或
self.performSegue(withIdentifier:"id",sender:nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let destViewController = segue.destination as! NCMainAdmin
let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
secondViewcontroller.adminUserData = "This is a test"
【讨论】:
以上是关于Swift:将数据从视图控制器传递到导航控制器的主要内容,如果未能解决你的问题,请参考以下文章