使用导航控制器 Swift 在 ViewController 与另一个 ViewController 之间传递数据 5

Passing data between a ViewController to another ViewController with navigation controller Swift 5

我希望你会很棒,关键是我想在 SeondViewControllerHomeViewController 之间传递数据并且它们之间存在导航控制器,但不幸的是,我不能。我尝试使用 present call 功能,但模拟器卡在 SecondView,这是我的观点照片:

我的 secondViewController 将数据传递给 homeViewcontroller 的按钮代码是:

   @IBAction func nextStep(_ sender: Any) {
    
    let thirdVC = HomeViewController()
    thirdVC.cid = cityId
      // present(thirdVC, animated: true, completion: nil)
    }

另外,我指向它的 homeViewController 代码在这里:

    class HomeViewController: UIViewController {

     var txt : String = "  "
     var cid : String = "  "
    
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view.
         print("\n iiiiiiiiiid is \(cid) ")
    }

感谢您的关注。

如果你使用的是 segue,那么在 storyboard 中添加“segue_identifier”,然后在 secondviewcontroller 中添加:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
             if segue.identifier == "segue_ identifier" {
                let mainTab = segue.destination as! tabBarController
                let nav = mainTab.viewControllers[0] as! UINavigationController
                let vc = nav.viewControllers.first as! HomeViewController
                vc.cid = cityId
            }
        }

因为你的 segue 目标是 UINavigationController,你需要像这样发送数据到视图控制器