iosswiftunwind-segue

How do I return to my primary display (A) if I go from screen A to B to C back to B using unwindSegue


I currently am learning how to program in Swift and have made 3 different pages; Pages A, B, and C. From page A, I am able to navigate to page B, and from page B, I am able to go to either page C or page A. When I go to page C I can go back to page B or page A.

The issue that I am running into however is that if I go from Page A to Page B to Page C back to Page B, when I try to close Page B, it would display Page C briefly before closing back to page A.

All of these are on separate viewcontrollers and I've just been presenting all of them modally each time

I've tried using Unwind Segues Step-by-Step and Create Unwind Segues in Swift 3 to understand what I've been doing wrong, however from what I've read on them (and from my limited knowledge of Swift), none of them discuss going back a single page and then trying to close after. They all go from 1 -> 2 -> 3 back to 1 without the intermediate step of going back to page 2.

@IBAction func close (_ unwindSegue: UIStoryboardSegue){} is what I've been using as my unwindSegues linking these up to my Exit placeholder. This was done on both Pages B and C

Currently there is no error message, it appears to work but it does show that intermediate screen which is not what I am looking for.

Thanks!


Solution

  • You said:

    when I tap on the back button on page C -> B it shows that I trigger an Action that presents Modally (if that helps)

    That is precisely the problem. Having gone from A to B to C, C should not then modally present B again. You’re creating a new, second instance of B. You’d have a view controller hierarchy that looks like:

    A » B1 » C » B2

    If, though, you successfully dismissed/unwound from C to B, you would have ended up with only A and B in the view controller hierarchy:

    A » B

    And then, when B went to dismiss/unwind to A, C would be long gone and there’s no way you’d see it in your animation.


    If you’re using unwind segues, I’d suggest that view controller A had an unwind action like so:

    @IBAction func unwindToA(_ segue: UIStoryboardSegue) { }
    

    And view controller B could have an unwind action like so:

    @IBAction func unwindToB(_ segue: UIStoryboardSegue) { }
    

    Now, when C wants to unwind back to B, if you could use the latter unwind action. If you want to unwind to A (either from B or C), you could use the former one.

    Just make sure that you never present/show when you want to go backwards in the view controller hierarchy. Either use unwind segues, or dismiss/pop as appropriate.