2021年1月3日星期日

Accessing properties of the root controller on a UINavigationController stack

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.

https://stackoverflow.com/questions/65554591/accessing-properties-of-the-root-controller-on-a-uinavigationcontroller-stack January 04, 2021 at 04:27AM

没有评论:

发表评论