diff --git a/Sources/SwiftUINavigator/Navigator.swift b/Sources/SwiftUINavigator/Navigator.swift index 977f3cb..0c9a1f9 100644 --- a/Sources/SwiftUINavigator/Navigator.swift +++ b/Sources/SwiftUINavigator/Navigator.swift @@ -11,6 +11,9 @@ import UIKit public class Navigator { public weak var viewController: UIViewController? + var navigationController: UINavigationController? { + viewController?.navigationController + } public func set(_ viewController: UIViewController?) { self.viewController = viewController @@ -26,7 +29,7 @@ public extension Navigator { func present(_ destination: UIViewController, style: UIModalPresentationStyle = .fullScreen, animated: Bool = true) { destination.modalPresentationStyle = style - self.viewController?.navigationController?.topViewController?.present(destination, animated: animated) + navigationController?.topViewController?.present(destination, animated: animated) } } @@ -34,44 +37,43 @@ public extension Navigator { public extension Navigator { func popToRootViewController(_ animated: Bool = true) { - viewController?.navigationController?.popToRootViewController(animated: animated) + navigationController?.popToRootViewController(animated: animated) } func pop(animated: Bool = true) { - self.viewController?.navigationController?.popViewController(animated: animated) + navigationController?.popViewController(animated: animated) } func pop(to controller: AnyClass, animated: Bool = true) { - let target = self.viewController?.navigationController?.viewControllers + let target = self.navigationController? + .viewControllers .filter { $0.isMember(of: controller) } .first guard let target = target else { return } - self.viewController?.navigationController?.popToViewController(target, animated: animated) + popToViewController(target, animated: animated) } func pop(to identifier: String, animated: Bool = true) { - let target = self.viewController?.navigationController?.viewControllers + let target = self.navigationController? + .viewControllers .filter { $0.identifier == identifier } .first guard let target = target else { return } - self.viewController?.navigationController?.popToViewController(target, animated: animated) + popToViewController(target, animated: animated) } func pop(toOneOf controllers: AnyClass..., animated: Bool = true) { - guard let viewControllers = self - .viewController? - .navigationController? - .viewControllers + guard let viewControllers = navigationController?.viewControllers else { return } for controller in viewControllers { let shouldPop = controllers.contains(where: { controller.isMember(of: $0) }) guard shouldPop else { continue } - self.viewController?.navigationController?.popToViewController(controller, animated: animated) + popToViewController(controller, animated: animated) } } @@ -88,15 +90,23 @@ public extension Navigator { .first guard let target = target else { return } - self.viewController?.navigationController?.popToViewController(target, animated: animated) + popToViewController(target, animated: animated) + } + + func popToViewController(_ controller: UIViewController, animated: Bool = true) { + navigationController?.popToViewController(controller, animated: animated) } + func dismiss(animated: Bool = true) { - self.viewController?.dismiss(animated: animated) + viewController?.dismiss(animated: animated) } func removeFromParentAndSuperView() { - self.viewController?.navigationController?.topViewController?.removeFromParentAndSuperView() + viewController? + .navigationController? + .topViewController? + .removeFromParentAndSuperView() } } #endif