Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

I am having trouble parsing this. How are you storing data in a UITabBarController? Are you subclassing UITabBarController? The tab bar controller is really only a meta controller used to control view controllers. In Apple's MVC framework, view controllers should be used to bind model data to views. If you need to share data between view controllers in a complex way, then you can use notifications, create a singleton, store the data in an app delegate, or use a shared external store like CoreData. Jeffery Thomas May 19, 2012 at 1:27 Jeff, so be specific, I pass the data to the UITabBarController, however I don't understand how to distribute the data to the separate tabbed view controllers austin May 24, 2012 at 2:15

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!

Couldn't have dreamed of an easier solution. Gold!! This covers the solution about and discusses possible issues with more than 5 tabs. makeapppie.com/2015/02/04/… – DogCoffee Aug 6, 2015 at 21:45

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.
                i'm having trouble getting this to work. i've tried putting the tabBarController code in its viewDidLoad and in its tabBarController:didSelectViewController, no success in either. I put the child code in its viewDidLoad
– austin
                May 24, 2012 at 2:10

If you got an UINavigationController as one of your UITabBarController view controllers, you can do the following:

NSArray *viewControllers = [self.tabBarController viewControllers];
UINavigationController *myNavController = (UINavigationController *)viewControllers[2];
MyViewController *myController = [[myNavController childViewControllers] firstObject];
// Remember to alloc/init objects first if the objects haven't been initialized already.
[myController.whateverArray = [[NSArray alloc] initWithArray:@[@"Example1", @"Example2"]];
[self.tabBarController setSelectedIndex:2];

Using Swift

If you have UINavigationController as one of the tabs in UITabBarController, you want to get the instance of the child view controller instead of creating a new instance and push it to the navigation view controller before changing the tab view index. You can do this...

This sample code assumes the Navigation Controller is at the first tab (index 0) and "childViewController" is embedded in the Navigation Controller.

let viewControllers = self.tabBarController?.viewControllers
let navController = viewControllers![0] as! UINavigationController
let profileViewController = self.storyboard?.instantiateViewControllerWithIdentifier("childViewController") as? ProfileViewController
profileViewController!.parameter1 = "Value1"
profileViewController!.parameter2 = "Value2"
navController.pushViewController(profileViewController!, animated: true)
self.tabBarController?.selectedIndex = 0

With Swift:

if let navController = self.tabBarController?.viewControllers?[1] as? UINavigationController{
        if let testController = navController.childViewControllers.first as? MyViewController{
            testController.data = data
            self.tabBarController?.selectedIndex = 1

Got this working...

I have 3 view controller, I have to pass the array from one to another..

Here is what I did

Appending the data to one of Tab View Controller

NSArray *viewControllers = [self.tabBarController viewControllers];
    ListViewController *listView = (ListViewController *)viewControllers[1];
    listView.myArray = self.myArray;
    [self.tabBarController setSelectedIndex:1];

And after that, load the view controller, that's it

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ListViewController *listView = (ListViewController *)[storyboard instantiateViewControllerWithIdentifier:@"IDListView"];
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.