iOS tabbed application with table view, how to reset table view tab? Beginner.

408 views Asked by At

I have a tabbed application with 3 tabs

The first tab is a table view The second and third tabs are single page views

When a user clicks on a table cell in view one a new view is pushed on with a back button to the table cell.

Now lets say they click tab 2, then back to tab 1. The new view that was pushed on tab 1 is still visible. What I would like is to have the table view "reset" when they navigate away from it with another tab so that when they return they are presented with the table view instead of the new view that was pushed on .

2

There are 2 answers

0
agilityvision On BEST ANSWER

I agree with ElJay comment, but to answer the question use UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

when a new tab is selected see if the current one is tab 1 selectedIndex property of tabBarController and if so popToRootViewController:

0
Duncan C On

As the others have said, this is probably not a good user experience. The idea of a tab controller is that it lets the user switch freely between the different parts of their app and go right back to what they were doing before.

If you're determined to do it this way, then make your navigation view controller's root view controller a custom subclass, and set it up as the tab bar controller's delegate.

In your custom view controller, implement the function shouldSelect(), as below:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
{
  if viewController == self.navigationController {
    self.navigationController.popToRootViewController()
  }
  return true
}

Disclosure: I haven't tried to compile the code above, much less test it.