简体   繁体   English

通过导航在 ViewController 之间传递数据到另一个 ViewController controller Swift 5

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

I hope you'll be great, the point is I wanna pass data between SeondViewController to HomeViewController and navigation controller exist between them, but unfortunately, I can't.我希望你会很棒,关键是我想在SeondViewControllerHomeViewController之间传递数据,并且导航 controller 存在于它们之间,但不幸的是,我不能。 I try present call function it's work but the simulator stuck in SecondView , and here is my views photo:我尝试现在调用 function 它的工作但模拟器卡在SecondView ,这是我的意见照片:

故事板

My secondViewController button code to pass data to homeViewcontroller is:我将数据传递给homeViewcontrollersecondViewController按钮代码是:

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

Also, my homeViewController code that I point to it, is here:另外,我指向它的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) ")
    }

Thanks for your attention.感谢您的关注。

If you are using segue, then add "segue_identifier" in storyboard and the in secondviewcontroller add:如果您使用的是 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
            }
        }

Because your segue destination is UINavigationController, you need to send data to view controller like this因为你的segue目的地是UINavigationController,你需要像这样发送数据查看controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM