Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A navigation controller that does a better job at communicating navigation events
//
// PleasantNavigationController.swift
// NavControllerTest
//
// Created by Antonio Nunes on 23/08/2018.
// Copyright © 2018 SintraWorks. All rights reserved.
//
import UIKit
@objc protocol PleasantNavigationControllerDelegate: AnyObject {
@objc optional func willPushViewController(_ viewController: UIViewController, animated: Bool)
@objc optional func didPushViewController(_ viewController: UIViewController, animated: Bool)
@objc optional func willPopViewController(animated: Bool)
@objc optional func didPopViewController(animated: Bool)
@objc optional func willPopToViewController(_ viewController: UIViewController, animated: Bool)
@objc optional func didPopToViewController(_ viewController: UIViewController, animated: Bool)
@objc optional func willPopToRootViewController(animated: Bool)
@objc optional func didPopToRootViewController(animated: Bool)
@objc optional func willSetViewControllers(_ viewControllers: [UIViewController], animated: Bool)
@objc optional func didSetViewControllers(_ viewControllers: [UIViewController], animated: Bool)
}
class PleasantNavigationController: UINavigationController, UINavigationControllerDelegate {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
}
var pleasantDelegate: PleasantNavigationControllerDelegate? {
return delegate as? PleasantNavigationControllerDelegate
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
pleasantDelegate?.willPushViewController?(viewController, animated: animated)
super.pushViewController(viewController, animated: animated)
pleasantDelegate?.didPushViewController?(viewController, animated: animated)
}
@discardableResult
override func popViewController(animated: Bool) -> UIViewController? {
pleasantDelegate?.willPopViewController?(animated: animated)
defer { pleasantDelegate?.didPopViewController?(animated: animated) }
return super.popViewController(animated: animated)
}
@discardableResult
override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
pleasantDelegate?.willPopToViewController?(viewController, animated: animated)
defer { pleasantDelegate?.didPopToViewController?(viewController, animated: animated) }
return super.popToViewController(viewController, animated: animated)
}
@discardableResult
override func popToRootViewController(animated: Bool) -> [UIViewController]? {
pleasantDelegate?.willPopToRootViewController?(animated: animated)
defer { pleasantDelegate?.didPopToRootViewController?(animated: animated) }
return super.popToRootViewController(animated: animated)
}
override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) {
pleasantDelegate?.willSetViewControllers?(viewControllers, animated: animated)
super.setViewControllers(viewControllers, animated: animated)
pleasantDelegate?.didSetViewControllers?(viewControllers, animated: animated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.