0
votes

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
The problem is that there is no UIViewController code method that does what an unwind segue in the storyboard does. And writing your own would be very, very difficult. I've added that to my answer. See also my answer to stackoverflow.com/questions/62152164/unwind-without-storyboard. - matt

2 Answers

1
votes

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.

0
votes

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)