I recently found myself in a situation where I had presented a UINavigationController modally and then pushed UIViewControllers within the UINavigationController. On the last UIViewController, I had two scenarios that I wanted to handle: 1) navigate back to the first UIViewController within the UINavigationController’s stack and 2) dismiss the UINavigationController, including all the pushed UIViewControllers contained in the UINavigationController stack.
After some initial trial & error, I decided to spend some time understanding the different ways in which both UINavigationControllers and UIViewControllers handle navigation.
First lesson learned:
Any UIViewController (including UINavigationControllers as UINavigationControllers are a type of UIViewControllers) presented modally must be dismissed.
Second lesson learned:
Since UINavigationControllers hold UIViewControllers in their stack, they can present UIViewControllers both modally and via a push. When navigating away from any pushed UIViewControllers, UINavigationControllers must popthe UIViewController.
Third lesson learned:
Though it is possible to present UIViewControllers both modally and via a push, UIViewControllers can only present other UIViewControllers. Therefore, UIViewControllers can only be dismissed (unless they are inside a UINavigationController stack and the UINavigationController is handling the pop — see lesson #2). This is why it is not possible to call popViewControlleron a UIViewController.
Fourth lesson learned:
PresentingViewController is a property provided by Apple that is available on both UIViewControllers and UINavigationControllers. Calling presentingViewController.dismiss(animated:completion) will dismiss everything presented by the parentViewController. The parentViewController is the UIViewController that presented the current UIViewController. So, if you have a UINavigationController with 3 UIViewControllers within its stack, calling presentingViewController.dismiss(animated:completion) will dismiss both the UINavigationController and the 3 UIViewControllers held within its stack.
Fifth lesson learned:
Similarly, Apple also provides another property on UIViewControllers and UINavigationControllers — presentedViewController. Calling presentedViewController.dismiss(animated:completion) will dismiss the childViewController. The childViewController is the current UIViewController that is being presented.
Prior to this recent investigation, I always thought that navigation was relatively basic. However, when faced with a combination of pushed & presented UIViewControllers, I realized how important it is to fully understand the different navigation patterns.
As always, I hope this was helpful & happy coding!! :)
origin: https://medium.com/
Comments
Post a Comment