push initial view for UITabBarController when switching tabs

Discussion in 'iOS Programming' started by pashik, Aug 16, 2008.

  1. pashik macrumors member

    Joined:
    Jul 16, 2008
    #1
    I have UITabBar based application with 3 tabs which are UINavigationControllers.

    When user clicks certain button on tab1 i push another viewcontroller (so there is Back button in navigationbar).
    then i click on tab2 and back to tab1. State of pushed viewcontroller is the same (backbutton is visible).
    How i can make so that when user clicks on tab1 again - it will have initial view (not pushed view)?

    thanx
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Easiest solution would be to implement the UITabBarControllerDelegate method tabBarController:didSelectViewController: and then call popToRootViewControllerAnimated: on the navigation controller.
     
  3. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #3
    Doesn't work.
    here is hierarchy of controls:
    UITabBarController
    -UITabBar

    -UINavigationController (shown in tab1)
    --UINavigationBar
    --UIViewController (<UITableViewDelegate,UITabBarControllerDelegate>)
    ---UIView
    ----UITableView

    -UINavigationController (shown in tab2)

    -UINavigationController (shown in tab2)

    In didSelectRowAtIndexPath i push another UIViewController from different NIB.
    If i try to add didSelectViewController into UIViewController - it doens't work.

    How i can change view to initial state (not pushed) after switching between tabs?
     
  4. santelia macrumors newbie

    Joined:
    Aug 17, 2008
    #4
    Do you get error or no effect at all?
     
  5. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #5
    There is no any error and no effect at all.
     
  6. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #6
    i attached example of application.

    if u click on any item row in list - new view will be pushed.
    then click on another tab and again on List tab.
    i need to show view with tableview and not details view (though if u click on list 2 times - tableview will be shown)

    How it can be done?

    thanx
     
  7. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    I'll just give you code from my project which has worked for me:
    Code:
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
        UINavigationController *navController = (UINavigationController *)viewController;
        if (![navController isKindOfClass:[UINavigationController class]])
            return;
    	
        // did become inactive
        if (m_activeViewController != nil && [m_activeViewController respondsToSelector:@selector(viewControllerDidBecomeInactive)])
            [m_activeViewController viewControllerDidBecomeInactive];
        // set active
        m_activeViewController = navController.topViewController;
        // did become active
        if ([m_activeViewController respondsToSelector:@selector(viewControllerDidBecomeActive)])
            [m_activeViewController viewControllerDidBecomeActive];
    }
    m_activeViewController is an ivar which conforms to a custom protocol (that implements viewControllerDidBecomeActive and viewControllerDidBecomeInactive). It allows each view controller in the tab (more specifically the top view controller in each navigation controller) to run code when it becomes active or inactive. If you setup something like this you could get it to work.
     
  8. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #8
    The problem is that when i added UITabBarControllerDelegate into my UIViewController and added - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

    this method is not called.
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Did you set the tab bar controller's delegate?
     
  10. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #10
    yes, i did, u can see source code in attached example.

    Problem was that i didn't connect UITabBar in xib with controller class.
    so it is solved
     
  11. lrusty macrumors newbie

    Joined:
    Aug 24, 2008
    #11
    Hi pashik

    Can you please share the code? I am trying to create an application where the tab bar is displayed only after a few screens go by! Tried many things but to no avail!! Please help!

     

Share This Page