0

I'm studying for the iOS Developer Nanodegree from Udacity and I can't figure out how to pass information between view controllers.

In this video, the instructor ask that we present view controllers using code, segue & code, and segue.

https://www.youtube.com/watch?v=zGzu5PcP8TI

I spent hours trying to understand this but I'm not getting anywhere. Is there any resources that clearly explains this?

dan
  • 9,695
  • 1
  • 42
  • 40
Jack Ngai
  • 157
  • 1
  • 9
  • It's best to show us some code that you have tried so far and then we can tell you how to fix it. You don't need to dump all of your code -- just the important parts. – Lou Franco Apr 11 '16 at 22:00

1 Answers1

0

Let's say you have VC1 and VC2 (two UIViewController classes) and that they are both in the same Storyboard file.

VC1 and VC2 both have a property:

var aProperty: String 

VC1 has a UITextField, and when it changes, you set aProperty to the text field's text.

Now, you want to show VC2 and VC2 needs the value of aProperty from VC1.

If you set up a segue from VC1 to VC2, when it is triggered, this function in VC1 will be called (if it exists)

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // You can set up VC2 here
 }

For example:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     if let vc2 = sender as? VC2 {
          vc2.aProperty = vc1.aProperty
     }
 }
Lou Franco
  • 87,846
  • 14
  • 132
  • 192