Perform Segue backwards

Hello,


my project achritecture has some strange behavior:

In the storyboard I connected some View Controllers via segues.

So when one of the previous View Controllers were already loaded and the current View Controller performs a segue to a previous one,

I can see in the console log that "two" of them live in the back-stack now - e.g. a method that is triggered through the Notification Center

this method is executed twice now.

Are segues backwards force new instances of previous View Controllers?

It is not a scenario where I just could call "dismiss" or a Navigation View Controller. It is a scenario with several View Controllers and a segue could be e.g. VC number 8 to VC number 1.

I think the segue uses "push", however can I somehow dismiss a VC when it is left with a segue -> it would be no longer on the back-stack!?


Greetings


Tobias

Accepted Reply

Do you create a new segue to go back from Two to One ?

If so, you create a new instance of first controller.


The solution is to use unwindSegue


In first controller, add the IBAction


@IBAction func unwindToFirstViewController(_ sender: UIStoryboardSegue) {
     // No code needed, no need to connect the IBAction explicitely
    }

In controller Two (wherre you go through the push), connect the button that returns you to First to the Exit button :

- COntrol drag from the button to the top right icon of the ViewController Two (in IB)

- you should see a pop up

- select unwindToFirstViewController


That's it.

Replies

Do you create a new segue to go back from Two to One ?

If so, you create a new instance of first controller.


The solution is to use unwindSegue


In first controller, add the IBAction


@IBAction func unwindToFirstViewController(_ sender: UIStoryboardSegue) {
     // No code needed, no need to connect the IBAction explicitely
    }

In controller Two (wherre you go through the push), connect the button that returns you to First to the Exit button :

- COntrol drag from the button to the top right icon of the ViewController Two (in IB)

- you should see a pop up

- select unwindToFirstViewController


That's it.

Works great. Very Thanks!