Domanda

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)?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top