Swift create a unwind segue programmatically

158 Views Asked by At

I'm creating a unwind segue using storyboard, following the instruction here.

But what I want to do is to create that unwind segue programmatically using swift,

The reason behind this is I want to create a default unwind segue to all of my UIViewController.

2

There are 2 best solutions below

2
Frankenstein On

If you mean to go back to the root view controller you can use popToRootViewControllerAnimated on navigationController like this:

navigationController?.popToRootViewController(animated: true)

Or if you wish to pop back to a particular view controller then use:

navigationController?.popToViewController(secondViewController, animated: true)
0
matt On

You can’t. A segue is something in the storyboard. Your code (programmatically) cannot see into the storyboard and change it.

Instead, you can do programmatically what the unwind segue did, such as pop back to a certain view controller.

But the general problem of "returning" to a certain view controller no matter what that may involve (some combination of dismiss and pop, etc.) is a very difficult and common one. It is certainly extremely annoying that, although a storyboard unwind segue "knows" how to do that, there is no method that lets you do that in code. I can only suggest filing a bug report with Apple.