Quick Tip: How to dismiss all presented view controllers?

If you are quite new in Swift, sooner or later you will face such view hierarchy:

So when app starts, you are using Dashboard view controller as root, and if user is not authorized, you need to show Login view. In case you are pushing Login view from navigation controller, case is simple, but if you need to use “present”, it is not, because calling “dismiss” on view controller is causing to dismiss only this specific view. In our case calling “dismiss” on Register view, will remove it but instead of dashboard, user will still see Login page. To solve this, you need to dismiss all view controllers from Dashboard which is first one using:

And how Dashboard will know is user is logged in? In simplest form, you can use NotificationCenter, so in Dashboard you may add:

and later in you controller code:

Also for notification itself, you need to register its name:

And finally in Register view after user has registered successfully and is authorized, all you have to do is call:

Of course there are more elegant ways to communicate between controllers, but this one is simplest.

Leave a Reply