Handling Navigation Cases with a UIViewController Extension

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

  1. navigateToViewController: Pushes a view controller onto the navigation stack.
  2. presentViewController: Presents a view controller modally.
  3. popViewController: Pops the current view controller from the navigation stack.
  4. dismissViewController: Dismisses the current view controller if presented modally.
  5. popToRootViewController: Pops all view controllers from the navigation stack, returning to the root view controller.
  6. 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.

A pat on the back !!