3

my storyboardI'm having trouble accessing my view controllers under the tab bar controller. Here is what my storyboard looks like:

  1. View Controller A (-> Page View Controller -> View Controller C
  2. View Controller A -> Tab Bar Controller (MyTabBarController.h/.m) -> Navigation Controller (MyNavigationController.h/.m)-> View Controller B (TabViewController.h/.m)
  3. Tab Bar Controller (MyTabBarController.h/.m) -> View Controller D
  4. Tab Bar Controller (MyTabBarController.h/.m) -> View Controller E

From View Controller A I have an IBAction called loginButton that is connected to the Tab Bar Controller, and currently it looks like this:

- (IBAction)loginButton:(id)sender {

    MyNavigationController *localNavigationController;

    UIStoryboard * storyboard = self.storyboard;

    MyTabBarController *tbc = [[MyTabBarController alloc] init];

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];

    TabViewController *login = [storyboard instantiateViewControllerWithIdentifier: @ "TabViewController"];

    localNavigationController = [[UINavigationController alloc] initWithRootViewController:login];

    localNavigationController.delegate = self;

    [localControllersArray addObject:localNavigationController];


    tbc.viewControllers = localControllersArray;


    tbc.delegate = self;
    tbc.moreNavigationController.delegate = self;


    tbc.selectedIndex = 0;

    [self presentViewController:tbc animated:YES completion:^{

    }];

}

I'm not able to get this displayed correctly. I am getting a bunch of warnings in this piece of code. and it is also not showing the different tab items in the bottom of the Tab Bar, even though I have put images/text on each tab.

So how do I display/access the view controllers inside the Tab Bar Controller correctly? (ie View Controllers C/D/E)?

  • 1
    It's really unclear what you're trying to do. If you're using a storyboard, and it has those various controllers in it, then why are you instantiating a tab bar controller and a navigation controller in code? You should be able to create all this in the storyboard. It would be helpful if you could post an image of your storyboard. Also, is one of the controllers, A, B, C, or D, the same as "login" – rdelmar Dec 20 '13 at 01:50
  • @rdelmar I am new at this and I am trying to understand how various view controllers work together. I wrote the code because I couldn't call the screen under the tab bar. Without instantiating tab bar controller/navigation controller/tab view controller, nothing was shown. I could simply skip the tab bar controller and navigation controller and simply reveal the last view controller (C), but I really would like to understand how to call these controllers from the first controller, which is a view controller. –  Dec 20 '13 at 08:38

3 Answers3

4

The storyboard that you show in your question already contains the tab bar controller, navigation controller, and login controller properly hooked up to each other. Because of that, you shouldn't be instantiating a new tab bar controller or navigation controller in code -- they will be instantiated by the storyboard when you instantiate the tab bar controller. So, the only thing you need to do, is to give the tab bar controller in the storyboard an identifier, and do this (assume the identifier is called MyTabBarController):

- (IBAction)loginButton:(id)sender {
    UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyTabBarController"];
    [self presentViewController:tbc animated:YES completion:nil];
}

You wouldn't even need this code if you control drag from the "Login" button to the tab bar controller, and choose "Modal". That will create a modal segue which will present the tab bar controller with no code at all.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • @LienneNguyen, Good, I hope this helped you out. If so, you should accept my answer to close out this question. – rdelmar Dec 20 '13 at 21:26
1

If you just want to select another tab from the tabBar controller then use something like this:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:3];

Note that if the tabBar controller is the initial view controller you can grab an instance of it it the applicationDidFinishLaunching method and store it in the AppDelegate. Then you'll be able to access it like this:

 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

Remember to import the AppDelegate.h

Stas Zhukovskiy
  • 799
  • 9
  • 21
0

I recommand you to use a Singleton shared instance to share multiple informations form multiple controllers. It's a good design Pattern for you usage. I'm writing samples of Design Patterns usage on cocoa (see https://github.com/leverdeterre/DesignPatterns -> Real singleton)

user1226119
  • 324
  • 3
  • 6
  • OK, I will look into what a singleton is and check out your github right now :) I have no idea what that is! I definitely will need to pass data around all these controllers will need that! –  Dec 20 '13 at 19:10
  • I googled around and found that most people recommended not to use singletons because singletons are globals. Instead we should be using dependency injections as it's a simpler solution. –  Dec 20 '13 at 23:28