Unheilig
Unheilig

Reputation: 16292

iOS: tab bar view controller using storyboard

I am trying to accomplish the following:

What I would like to do is:

1.) When the user clicks on a button in UIViewController, it will retrieve a list of items.
2.) The items will then be displayed in UITableView.
3.) Lastly the number of items will be displayed on the third view (not initiated by UITableView)

All the above I manage, except that I would like to wrap all these controllers in a customized tab bar controller. But the data is not being passed from the UIViewController to the UITableView or the other View. I am using Storyboard, I hooked up tab bar controller and the other 3 controllers there as view controllers. I have tried instantiating UITableView and the UIView controllers using UIStoryboard method and creating instances of these controllers and send the data to them, the controllers are shown in tab bar controller, but data is not. I am not using Segue, by the way. Am I missing something here?

Thanks.

CODE:

UITabBarController tabBarController = (UITabBarController) self.window.rootViewController;

    UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    mainViewController *mainController = (mainViewController*)[storyboard1 instantiateViewControllerWithIdentifier:@"mainController"];

    UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    tableViewController *tableview = (tableViewController*)[storyboard2 instantiateViewControllerWithIdentifier:@"tableviewController"];

    UIStoryboard *storyboard4 = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    resultController *mapView = (resultController*)[storyboard4 instantiateViewControllerWithIdentifier:@"resultView"];</code>

I have tried also creating the UI Table View and the result view controller in MainViewController and pass data to them (before they are even displayed), but no data is ever present when these controllers are displayed. I made sure I alloc and init these controllers properly and NSLog shows that the instance methods in these controllers are called, but still no data is shown, even though the controllers are displayed in tab bar view controller, when they are displayed.

UPDATE 2:

Now I am trying to do the following:

self.tableviewController = [self.tabBarController.viewControllers objectAtIndex:1];
self.resultViewController = [self.tabBarController.viewControllers objectAtIndex:2];

And when I call these:
NSLog(@"%@", [self.tabBarController class]);
NSLog(@"%@", [self.tableViewController class]);
NSLog(@"%@", [self.resultViewController class]);

They are all null.

Basically, I have 3 View Controllers hooked up as "view controllers" using storyboard to a UITabbarController. The main view controller (at index 0 in UITabbarController), from which I call the above, simply has one UIButton. Once pressed it will fetch a list of strings and send it to the Table View (at index 1 in UITabbarController) and on the last View Controller (at index 2 in UITabbarController) it will show just the number of strings as a result, which it receives from the Main View Controller, not from Table View.

I am not using any UINavigationController in my set up.

I would like to be able to fire off Table View and the Result View Controller before they are even displayed by clicking on the bar item tab on UITabbarController. By firing off I mean the data will be sent over to Table View and Result View even though they are not yet displayed.

Upvotes: 0

Views: 2395

Answers (1)

ilmiacs
ilmiacs

Reputation: 2576

  1. The code you present is unnecessary and wrong for what you want to achieve. (Usually you should have one instance of UIStoryboard.) Read the docs.
  2. You do not write anything about how you try to achieve the communication between the view controllers. There are several methods. But use delegation. Read the docs.
  3. If you answer to a comment you should use the @ character followed by the SO user name so he is notified about your updates.

Upvotes: 0

Related Questions