Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass data from a tab bar controller to one of its tabs?

I have a UITabBarController set up in storyboard. I want to pass a dictionary of data from the tab bar controller for use in the appropriate child tab, which is a standard UIViewController.

This seems like a long questions to answer, but I really don't know where to start. I'm looking for the easiest way through this. Must I make the tabs function programmatically? Pointing me towards any useful developer documentation would also be much appreciated.

-Austin
p.s. i'm new and have so far depended on storyboard for viewcontroller transitions

like image 551
austin Avatar asked May 19 '12 00:05

austin


People also ask

How do I add more tabs to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

How do I make a tab bar controller?

We open up our Main. storyboard and go ahead and delete the pregenerated view controller. Dragging a "Tab Bar Controller" from the Object Library into the storyboard automatically creates two view controllers that are already in the tab bar controller's array of view controllers.


2 Answers

It took a couple of days, but I discovered a simple solution. In my TabBarController's viewDidLoad method, I can access and set attributes of my the tabbed subviews using

    [[self viewControllers] objectAtIndex:0] 

or whatever index you wish to access.

Saving that as an instance of my tab1_viewController gives me a pointer to the object, from which I can get and set my property values. This was initially unclear because storyboard created the tab_viewController objects for me. I never had a chance to initialize them the way I wanted to!

like image 159
austin Avatar answered Sep 21 '22 21:09

austin


Your best bet is to use notifications.

In your tab bar, you would do this:

NSDictionary *myDictionary; // Populate this with your data.  // Fire the notification along with your NSDictionary object. [[NSNotificationCenter defaultCenter] postNotificationName:@"Some_Notification_Name"                                                      object:myDictionary];         

Then in your child tab ViewController, you would "listen" for that notification.

[[NSNotificationCenter defaultCenter] addObserver:self                                           selector:@selector(handleNotification:)                                               name:@"Some_Notification_Name"                                             object:nil];  - (void)handleNotification:(id)object {   // Use your NSDictionary object here. } 
like image 27
melsam Avatar answered Sep 23 '22 21:09

melsam