gpt4 book ai didi

Swift:将数据从 View Controller 传递到导航 Controller

转载 作者:行者123 更新时间:2023-11-28 13:39:53 26 4
gpt4 key购买 nike

我想将数据从一个 View Controller 传递到另一个 View Controller ,但我在它们之间有一个导航 Controller ,所以有一个“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 是导航 Controller
  • NCMenuAdmin 是 Navigation Controller 的第一个 View Controller

谢谢!

最佳答案

对于你的代码prepare(for to trigger,你需要创建一个从sender vc到navigation的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:将数据从 View Controller 传递到导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56189640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com