0

Okay so I found some code from a YouTube video where data is passed from one viewController to another and it works fine but, I need it to pass across multiple view controllers and not just one to the other.

YouTube Video along with project in desc:https://www.youtube.com/watch?v=Kpwrc1PRDsg

Here is the Code:

class ViewController: UIViewController {

@IBOutlet weak var textField: UITextField!

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let nextVC: secondViewController = segue.destinationViewController as! secondViewController

    nextVC.recevedText = textField.text!
}

Then the secondViewController code:

class secondViewController: UIViewController {
var recevedText: String = ""

@IBOutlet weak var textLable: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    textLable.text = recevedText
}

I have heard of delegates before but I have a difficulty with them.

Joe
  • 59
  • 8
  • Okay I found out a possible good way to do it. Singletons. From here:http://stackoverflow.com/questions/25215476/how-do-you-pass-data-between-view-controllers-in-swift –  Sep 13 '16 at 01:37
  • Your question doesn't specify where the data is being sent. If you want to keep passing the data forward, just keep assigning the variables in prepareForSegue(). If you need to pass it back, read about delegates and protocols: http://stackoverflow.com/questions/24099230/delegates-in-swift – Shades Sep 13 '16 at 03:57

1 Answers1

0

If you need to pass on the value across multiple view controller, one possible way is to use global variables which can be updated and used in any of the view controller.

Caution: Global variables are tedious and you need to keep track of where you are updating and sequence of usage.

Pandurang Yachwad
  • 1,695
  • 1
  • 19
  • 29