How to access the View controller from the tab bar controller?

I have a tab bar controller, and each tab consists of a navigation controller.

How can I access a specific view controller in the navigation controller on a tab so that I can access a property specific to the view controller?

I tried the following code:

//Get the navigation controller of the 3rd tab
self.tabController.selectedViewController 
= [self.tabController.viewControllers objectAtIndex:2];

//Attempt to retrieve the viewcontroller I want from the tabcontroller
SomeViewController *svc = (SomeViewController *)self.tabController.selectedViewController;

//Attempting to access a BOOLEAN property in svc viewcontroller
svc.someProperty = YES;

The above code failed because it seems that "self.tabController.selectedViewController" returns a navigation controller to me. How can I extend the code to access "svc.someProperty"?

+5
source share
3 answers

self.tabController.selectedViewController - , , :

visibleViewController

, :

topViewController

SomeViewController, , :

viewControllers

+4

Since the view manager is the control of the returned UINavigationController (SVC), you can simply send the following method to it [svc topViewController];. This should return the view controller to you, and then you can access the property.

+1
source

All Articles