Unwinding Segues in SwiftUI

Image for post
Image for post
Give a shoutout to Marius Niveri on social or copy the text below to attribute.

The default navigation in iOS apps allow the user to push views on top of the stack. Once the view is on top of the stack, it is visible to the user. In order to go back to the previous screen, the view is simply popped off the stack.

In some cases the user needs to go from View C back to View A, without having to visit View B. This can be done by using a feature called Unwind Segues. Unwind segues are common in UIKit and can even be setup using UIKit storyboards.

Unfortunately, the process of setting up unwind segues in SwiftUI is not as straight forward. In this article, I will cover how to setup unwind segues in a SwiftUI application.

If you are interested in a video tutorial then you can watch using the YouTube video below:

We will start by implementing your ViewA. View A will consist of a NavigationLink, which takes you to View B as shown in the code below:

One thing to note is that we are passing a bindable property $isActive to View B. This is important as this property will allow us to eventually jump from View C to View A.

Let’s take a look at the View B implementation.

View B is very similar to View A. It simply displays a NavigationLink, which takes a user from View B to View C. View C initializer takes a bindable property as an input, just like View B.

Finally, let’s take a look at the View C implementation.

Inside View C we have a button, which when pressed changes the rootActive bindable property to false. This causes the a chain reaction and View B and View A properties are also set to false. By setting the properties to false, the views are popped out and immediately return to the root which is View A.

Image for post
Image for post

GitHub: https://github.com/azamsharp/UnwindSegues

If you like this article and want to support my work then check out my Udemy courses below:

iOS Developer, speaker and educator. Top Udemy and LinkedIn instructor. Lead instructor at DigitalCrafts. https://www.udemy.com/user/mohammad-azam-2/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store