ViewController Hierarchy getting 'lost'
I've got a view hierarchy which is setup (programmatically) as follows:
Window.root = TabBarController-->UINavigationControllers-->UIViewControllers
I presume that's rather standard. Here's开发者_高级运维 my problem:
I'm on Tab A. I want to navigate to Tab B, and call a method on the visibleViewController on Tab B.
// View Changes OK
[AppDelegate.tabBarController setSelectedIndex:tabB];
// nav = 0x387ABF i.e. Valid Address
UINavigationController *nav = (UINavigationController*)[AppDelegate.tabBarController selectedViewController];
// The problem:
nav.viewControllers; // this is nil
nav.topViewController; // as is this
nav.visibleViewContorller; // this too.
Even if I put the calls to nav.viewControllers
in a separate method which is called from the Main Thread, I still get 0x0
/nil
.
What am I doing wrong?
A follow-up question is: How can I pass information from one ViewController to another when changing tabs? (If I can't call methods on VC's from tabA to tabB)
I have a feeling it is related to my question here.
You should store the information in a common place, either a singleton or as you are a beginner just make a class and pass it down.
sharedDataObject = [[MySharedDataObject alloc] init];
firstViewController.myDataObject = sharedDataObject;
secondViewController.myDataObject = sharedDataObject;
精彩评论