Search code examples
iosswiftsegueswift4

How to pass data from First ViewController directly to the Third View Controller using segue?


I was able to perform the task of passing value from one view controller to another view controller. I had a textField and submit button in one view controller and in another I had a label, after I gave input to the textField, I was able to replace the label text with textField's text.

But now what I am trying is that when I click on submit button in first view controller , it should move to second view controller (performSegue) and at the same time it should pass the value to the third view controller..

But I am getting an error if I do that, is there a simple way to pass values from one view controller to third view controller where I can skip "n" number of view controllers which come in between the target and destination?

Here's an example:

Suppose there are three viewControllers, first viewController has a textField and a submit button, second view Controller has a "NEXT" button and third View Controller has a label.

When I enter the textField and click on submit , it should submit the textField.text to third View Controller and should perform segue to second view controller,

now when I am on second view controller it'll show NEXT button. After I click on next, it'll take me to third View controller where it'll show me the first View COntroller's textField's text in the label...

My question is, how to do that? I went through a lot of youtube videos of implementing segues but nobody talked about how to pass data from one view controller to another view controller by skipping some view controllers.

I hope you do understand my question.

Your time and help will be highly appreciated! Thank You


Solution

  • assign storybordID to your controller for ex here i have assigned "HomeVC" (it would be greate if you assign class name and storybordid same)

      let objthirdController = self.storyboard?.instantiateViewController(withIdentifier: "thirdControllerStoerybordID") as! thirdControllerclassname
      objthirdController.variable = self.variable
       self.navigationController?.pushViewController(objthirdController, animated: true)