Search code examples
iosuitabbarnavbar

iphone combined tab bar and navigationController


Im implementing a combined tab bar and navigation programatically, using the apple documentation,

it is not working when calling the initWithFrame,[goes black screen]; but if left as below code it works for showing main screen, with out the tab bar, and when using the tab bar goes black screen

here the code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(  NSDictionary *)launchOptions {                  
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
StartViewController *startViewControllerView = [[[StartViewController alloc] init] autorelease]; //ojo recomendado por apple!!!
VideosViewController* VideosViewController_ = [[[VideosViewController alloc] init] autorelease];
PhotosViewController* PhotosViewController_ = [[[PhotosViewController alloc] init] autorelease];
SocialViewController* SocialViewController_ = [[[SocialViewController alloc] init] autorelease];
self.pagesNavigation = [[[UINavigationController alloc] initWithRootViewController:startViewControllerView] autorelease];
self.pagesNavigation.navigationBarHidden = NO;
NSArray* controllers = [NSArray arrayWithObjects:VideosViewController_, PhotosViewController_, SocialViewController_, startViewControllerView, nil];
self.tabBarController.viewControllers = controllers;
[self.window addSubview:startViewControllerView.view];
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window makeKeyAndVisible];
  return YES;
}

so if left as shown above, it works but if I comment the addSubview and uncomment the initWithFrame, it doesnt work,,

  //[self.window addSubview:startViewControllerView.view];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

So, what Im i missing?, what would be the right way to call the initWithFrame?

thanks a lot!


Solution

  • Why are all your viewcontrollers auto released? You should probably be retaining them and releasing them only when you're done with them.

    As for your structure, I've found that building a single navigation controller for each tab in the tabbarcontroller, adding those to the controller, then adding the tabbarcontroller to the window works like this...

    AppDelegate.h

    property (nonatomic, retain) UITabBarController *tabBarController;
    property (nonatomic, retain) UINavigationController *firstNavController;
    property (nonatomic, retain) UINavigationController *secondNavController;
    property (nonatomic, retain) FirstViewController *firstViewController;
    property (nonatomic, retain) SecondViewController *secondViewController;
    

    AppDelegate.m

    firstViewController = [[FirstViewController alloc] someInitMethod:someArg];
    firstNavController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; 
    
    secondViewController = [[SecondViewController alloc] someInitMethod:someArg];
    secondNavController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; 
    
    tabBarController = [[UITabbarController alloc] init];
    
    NSArray *tabs = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
    
    [tabBarController setViewControllers:tabs animated:NO];
    
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];