diff --git a/Examples/CaseStudies/UIKit/ErasedNavigationStackController.swift b/Examples/CaseStudies/UIKit/ErasedNavigationStackController.swift index c77aae3e3..e2eb3fb37 100644 --- a/Examples/CaseStudies/UIKit/ErasedNavigationStackController.swift +++ b/Examples/CaseStudies/UIKit/ErasedNavigationStackController.swift @@ -81,6 +81,10 @@ private class NumberFeatureViewController: UIViewController { self.number = number super.init(nibName: nil, bundle: nil) title = "Feature \(number)" + + navigationDestination(for: String.self) { string in + StringFeatureViewController(string: string) + } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") @@ -89,10 +93,6 @@ private class NumberFeatureViewController: UIViewController { super.viewDidLoad() view.backgroundColor = .systemBackground - navigationDestination(for: String.self) { string in - StringFeatureViewController(string: string) - } - let numberButton = UIButton( type: .system, primaryAction: UIAction { [weak self] _ in @@ -137,6 +137,10 @@ private class StringFeatureViewController: UIViewController { self.string = string super.init(nibName: nil, bundle: nil) title = "Feature '\(string)'" + + navigationDestination(for: Bool.self) { bool in + BoolFeatureViewController(bool: bool) + } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") @@ -145,10 +149,6 @@ private class StringFeatureViewController: UIViewController { super.viewDidLoad() view.backgroundColor = .systemBackground - navigationDestination(for: Bool.self) { bool in - BoolFeatureViewController(bool: bool) - } - let numberButton = UIButton( type: .system, primaryAction: UIAction { [weak self] _ in diff --git a/Sources/UIKitNavigation/Navigation/NavigationStackController.swift b/Sources/UIKitNavigation/Navigation/NavigationStackController.swift index 54cc23f48..feda12692 100644 --- a/Sources/UIKitNavigation/Navigation/NavigationStackController.swift +++ b/Sources/UIKitNavigation/Navigation/NavigationStackController.swift @@ -338,14 +338,19 @@ ) return } - stackController.path.append(.lazy(.element(value))) + withUITransaction(\.stackController, stackController) { + stackController.path.append(.lazy(.element(value))) + } } public func navigationDestination( for data: D.Type, destination: @escaping (D) -> UIViewController ) { - guard let navigationController = navigationController ?? self as? UINavigationController + guard + let navigationController = UITransaction.current.stackController + ?? navigationController + ?? self as? UINavigationController else { reportIssue( """ @@ -442,4 +447,15 @@ } } } + + private extension UITransaction { + var stackController: NavigationStackController? { + get { self[NavigationStackControllerKey.self] } + set { self[NavigationStackControllerKey.self] = newValue } + } + } + + private enum NavigationStackControllerKey: UITransactionKey { + static let defaultValue: NavigationStackController? = nil + } #endif