When a UINavigationController pushes a view controller into view, the nav controller creates its own instance of that view controller behind the scenes. Using this nav controller delegate:
extension Main_ProfileViewController: UINavigationControllerDelegate {
// called just after the navigation controller displays a view controller’s view and navigation item properties
open func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
print(viewController)
}
}
That instance can be "determined". The console prints something like this:
<ElChapo.SomeRandomViewController: 0x7fbe8b003640>
That 14-character hash is different each time that view controller is pushed by the nav controller so clearly they are different instances each time. My question: is that specific instance somehow accessible without using singletons? By accessible, I mean accessible where I can run a delegate to it and call specific methods within that specific instance.
This is what I'm trying to accomplish:
To pop a navigation stack back to its root, I would execute this function from within the view controller at the top of the stack.
// pop to root
func popToRoot() {
self.navigationController?.popToRootViewController(animated: true)
}
This method must be called from within that specific instance of that view controller. If I call popToRoot() from another object, like the tab bar, by passing a delegate to it, it doesn't work because the delegate needs to find that exact instance to execute the pop.