iOS dev
iOS dev

Reputation: 2284

iPhone- How to get the main view (1st view) every time when we switching form one tab to another tab

In my iPhone app, I have used tabbarcontroller in that i have 4 tabs for four different items

In each tab i have different views navigate transaction in those.

my requirement is, when i switch form on tab to another tab by taping on tabbar item.

T*the main view (first view ) of each tab has to be appeared instead of currently working view.*

For ex:

  1. I select tab 3 there i do some operations and navigate to some 2nd view in the third tab.

  2. and then i secet tab 4 and then again tab3.. then the previously woriking view view 2 presents in tab3,

    1. But i need to present 1st view at any time when i select tab 3 (Same like in any other tabs)

how to do..

Upvotes: 0

Views: 93

Answers (2)

rdelmar
rdelmar

Reputation: 104082

Make a subclass of UINavigationController, change the class of any navigation controllers you have in your storyboard to that class, and put this code in that subclass:

-(void)viewDidDisappear:(BOOL)animated {
    [self popToRootViewControllerAnimated:NO];
}

Whenever you click on another tab, this method will be called, and it will reset the navigation stack back to the root view controller.

Upvotes: 3

jailani
jailani

Reputation: 2270

Set your view controller or appdelegate as delegate of your tabbarcontroller. implement this delegate function

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
  int selectedIndex = tabBarController.selectedIndex;
  NSArray *tabbarControllers = tabBarController.viewControllers;

  //Then replace tabbarControllers[selectedIndex] by your new view controller(with navigation controller) 

   tabBarController.viewControllers = Your replaced Array;
}

Upvotes: 0

Related Questions