iphone push view controller inside navbar inside tabbar

manuelBetancurt picture manuelBetancurt · Aug 21, 2011 · Viewed 7.5k times · Source

I have an app that has a tabBar with a navbar,

The tabBar is showing, and working, but when I want to go to another page inside one of the tabs, it doesnt load the new page,

here my app delegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    



self.tabBarController = [[[UITabBarController alloc] init] autorelease];


StartViewController *startViewControllerView = [[[StartViewController alloc] init] autorelease]; //ojo recomendado por apple!!!
PhotosViewController* PhotosViewController_ = [[[PhotosViewController alloc] init] autorelease];
VideosViewController* VideosViewController_ = [[[VideosViewController alloc] init] autorelease];
SocialViewController* SocialViewController_ = [[[SocialViewController alloc] init] autorelease];



NSArray* controllers = [NSArray arrayWithObjects: startViewControllerView, VideosViewController_, PhotosViewController_, SocialViewController_, nil];

    self.tabBarController.viewControllers = controllers;
self.pagesNavigation = [[[UINavigationController alloc] initWithRootViewController:startViewControllerView] autorelease];
self.pagesNavigation.navigationBarHidden = NO;

[self tabBarConfig];

[self.tabBarController setViewControllers:controllers animated:YES];


[self.window addSubview:self.pagesNavigation.view];





self.window.rootViewController = self.tabBarController;

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

    [self.window makeKeyAndVisible];

return YES;



}

it shows tabs ok,

but in one of the pages I push the new view with

[self.navigationController pushViewController:bigPhotoView animated:YES];

but it doesn't work.

So how to load the new view from my tab?

Answer

B Hendrick picture B Hendrick · Aug 21, 2011

UINavigationController is a stack of view controllers. Every view controller has a property called as navigationController, which represents the UINavigationController to which it belongs. Thus, if a view controller doesn't belong to a UINavigationController (in other words, if a view controller is not present in a stack), it's navigationController property would be nil.

If you want to have a view controller from which you could push new view controllers and pop view controllers, you need to create a stack (UINavigationController) first, push your view controller in this stack. Now since stack exists, we can keep pushing new view controllers in this stack.

Your bigPhotoView is not getting pushed probably because there is no UINavigationController existing for the current view controller (from which you are trying to push bigPhotoView). This could be verified as follows:

if (self.navigationController != nil) {
    [self.navigationController pushViewController:bigPhotoView animated:YES];
}

In the above case, you might not enter the if statement.

I prepared your function briefly. It looks something like this.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Create Start view controller.
    StartViewController *startController = [[StartViewController alloc] init];
    UINavigationController *startViewNavigationController = [[UINavigationController alloc] initWithRootViewController:startController];
    [startController release];

    // Similarly create for photos, videos and social...

    // Create an array of view controllers.
    NSArray* controllers = [NSArray arrayWithObjects:startViewNavigationController, photosViewNavigationController, videosViewNavigationController, socialViewNavigationController, nil];

    // Create our tab bar controller.
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];

    // Set the view controllers of the tab bar controller.
    self.tabBarController.viewControllers = controllers;

    // Release the startViewNavigationController, photosViewNavigationController, videosViewNavigationController, socialViewNavigationController...

    // I don't know what this does.
    [self tabBarConfig];

    // Add the tab bar controller to the window.
    [self.window addSubview:self.tabBarController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

Here is the official apple's documentation for UINavigationController