I have a UINavigationController
with root HomeViewController(). HomeViewController has a property named someString. I want to access someString from another view controller that has been pushed onto the navigation stack programmatically:
navigationController?.pushViewController(DetailViewController(), animated: true)
In the DetailViewController that wants to access someString I am doing this:
var homeViewController: HomeViewController!
override func viewDidLoad() {
super.viewDidLoad()
let navController = self.navigationController!
homeViewController = navController.viewControllers.first as? HomeViewController
}
And then to get the value of someString from within DetailViewController:
var stringFromRoot = homeViewController.someString
Is this the proper way to access properties of the root view controller in a UINavigationController? It does work in my testing, but if it has errors or drawbacks, or if there is a better way, I would like to know about that.