UITabBarController + UINavigationController help

Discussion in 'iOS Development' started by johnmabassa, Apr 1, 2009.

  1. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    I have UITabBar based application with 3 tabs which are navigation controllers.
    When I clicks on the first tab, the navigationController's view is loaded and from there if I presses a button, I am pushing another viewController to navigation controller's stack.(Let that view to remain as it is.)

    Now if I go to tab 2 I get another view, now if I return to my tab1 , ideally i should get the view of the very first viewController of the navigationController but I am getting the view of the pushed one. How to fix this issue?


    this is how my MainWindow.xib looks like

    Code:
    TabBarController
    
             - UINavigationController->
                           -ViewController1----->(Push)->ViewController2
             - UINavigationController
                           -ViewController3
             - UINavigationController
                           -ViewController4
    ViewController2
             -View
    When I click first tab view of ViewController1 is loaded and when a button is pressed view of ViewController2 is loaded......now when I click tab2... view of viewController3 is loaded, now I am going back to tab1.... ideally i should get the view of viewController1 but I am getting view of viewController2 only and i need to go back to the view of viewController1 by pressing back button

    Please Register or Log in to view images

  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    In your tab bar controller's delegate, override
    Code:
    - (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController;
    In that method, have that navigaiton controller pop back to its root view controller.

    One very easy way:
    Code:
    if(viewController == yourViewController){
    	//Assuming your view controller that the tab
    	//bar presents is a UINavigationController (it is in
    	//your example)
    	[yourViewController popToRootViewControllerAnimated:NO];
    }

    EDIT: Although, I have no clue why you thought this would automatically be done for you.
  3. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    I realize this is off-topic, but you should probably split some of those up into seperate XIBs.
  4. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    Hi sorry for not mentioning this before, I have already tried didSelectViewController method
    but when I try to see the the value of viewController...

    Code:
    NSLog("selected controller is %@",viewController);
    it just says <UINavigationController:some memory address> so I was confused how to use that comparison method....



    Anyways I found another way to do it
    Code:
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    	NSLog(@"selected is %@",viewController);
    	/*
    	if(tabBarController.selectedViewController)
    	{
    		NSLog(@"selected is %@",tabBarController.selectedViewController);
    	}*/
    	
    	
    	NSArray *vc= tabBarController.viewControllers;
    	for (int i = 0; i < [vc count]; i++) {
    		UINavigationController *nc = [vc objectAtIndex:i];
    		if (nc == tabBarController.selectedViewController) {
    			continue;
    		}
    		[nc popToRootViewControllerAnimated:NO];
    	}
    Please let me know your opinion and please give me some pointer to fix the first approach of comparing viewControllers.Thanks a lot for the inputs guys.
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I used that exact same method in Pocket Elf (AppStore), so I'm not sure why it isn't working for you. If I find time today, I'll add this functionality to my UITabBar sample projects in the tutorial section.
  6. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0

    That will be really helpful.

    -John

Share This Page