jazzedge
12/2/2017 - 6:03 AM

Swift - Segues

See: https://makeapppie.com/2016/06/27/using-segues-and-delegates-for-navigation-controllers-in-swift-3-0/

To move data from Foo One to Foo Two, we will override prepare(for segue:).

In your source VC add the following code:

// segue ViewController -> ViewControllerB
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
    if segue.identifier == "viewNext" {
        let viewControllerB = segue.destinationViewController as! ViewControllerB
        viewControllerB.dataPassed = labelOne.text
    }
}

Line 3 we create vc which is the pointer to the segue’s destination view controller. destinationViewController is of type AnyObject!. Swift will only assume vc is a type UIViewController, not our specific instance of it. We need to downcast to the correct object type with the as! operator, and make vc an instance of FooTwoController. Once we do that, we can access the colorString property in line 4, sending our current text to the property in the new controller.


Now comes the slightly more challenging part: getting stuff back. For that we will need a delegate.

 Using this method, the segue used for passing data back from (vcB) to (vcA) is ALSO added to the source of (vcA), as an @IBAction method (rather than, as could possibly be expected, added to the source of (vcB)).

// segue ViewControllerB -> ViewController
@IBAction func unwindToThisView(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? ViewControllerB {
        dataRecieved = sourceViewController.dataPassed
    }
}

	
Øyvind Hauge beat me to the same solution method, but as I had already started with a more detailed answer, I'll add it as well.

Let's say your two view controllers are named as follows:

Master/entry point: ViewController  (vcA)
Secondary view: ViewControllerB  (vcB)
You set up the segue from (vcA) -> (vcB) as you have done in you example

/* in ViewController.swift */   

// ...

// segue ViewController -> ViewControllerB
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
    if segue.identifier == "viewNext" {
        let viewControllerB = segue.destinationViewController as! ViewControllerB
        viewControllerB.dataPassed = labelOne.text
    }
}
The somewhat tricky step next is that, using this method, the segue used for passing data back from (vcB) to (vcA) is also added to the source of (vcA), as an @IBAction method (rather than, as could possibly be expected, added to the source of (vcB)).

/* in ViewController.swift */   

// ...

// segue ViewControllerB -> ViewController
@IBAction func unwindToThisView(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? ViewControllerB {
        dataRecieved = sourceViewController.dataPassed
    }
}
You thereafter connect say, a button in (vcB) to this unwind action in (vcA) via the manual Exit segue in (vcB)

Below follows a complete example of passing text from (vcA) to (vcB); (possibly) modifying that text via an UITextField, finally returning the (possibly) modified text to (vcA).

Below follows a complete example of passing text from (vcA) to (vcB); (possibly) modifying that text via an UITextField, finally returning the (possibly) modified text to (vcA).