Navigation in iOS apps often involves pushing, presenting, or popping view controllers. To simplify common navigation tasks, you can create a Swift extension for the UIViewController
class. In this article, we’ll explore how to create a custom extension that makes handling navigation cases more convenient.
Prerequisites
Before we dive into the code, make sure you have a basic understanding of Swift programming and have Xcode installed on your development machine.
The UIViewController Extension
We’ll start by creating an extension for UIViewController
that adds some useful navigation methods. Below is the Swift extension:
import UIKit extension UIViewController { // MARK: - Navigation Helpers func navigateToViewController(_ viewController: UIViewController, animated: Bool = true) { self.navigationController?.pushViewController(viewController, animated: animated) } func presentViewController(_ viewController: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) { self.present(viewController, animated: animated, completion: completion) } func popViewController(animated: Bool = true) { self.navigationController?.popViewController(animated: animated) } func dismissViewController(animated: Bool = true, completion: (() -> Void)? = nil) { self.dismiss(animated: animated, completion: completion) } func popToRootViewController(animated: Bool = true) { self.navigationController?.popToRootViewController(animated: animated) } func popToLastViewControllerOfType<T: UIViewController>(_ type: T.Type, animated: Bool = true) { if let viewControllers = self.navigationController?.viewControllers { for viewController in viewControllers.reversed() { if viewController.isKind(of: type) { self.navigationController?.popToViewController(viewController, animated: animated) break } } } } }
Understanding the Methods
navigateToViewController
: Pushes a view controller onto the navigation stack.presentViewController
: Presents a view controller modally.popViewController
: Pops the current view controller from the navigation stack.dismissViewController
: Dismisses the current view controller if presented modally.popToRootViewController
: Pops all view controllers from the navigation stack, returning to the root view controller.popToLastViewControllerOfType
: Pops to the last view controller of a specific class type.
Usage Example
To use these methods, you can call them within your view controllers. For instance, to pop to the last view controller of a specific class type, you can use:
self.popToLastViewControllerOfType(MyViewController.self)
Conclusion
Creating a UIViewController
extension for navigation cases is a helpful way to streamline your iOS app’s navigation. These methods make it easier to navigate between view controllers, whether you need to push, present, or pop. Implementing this extension in your project can save time and effort in handling navigation tasks.
Feel free to customize and expand this extension to suit your specific app requirements. With these tools at your disposal, managing navigation within your iOS app becomes a more efficient and organized process.