質問

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 .

役に立ちましたか?

解決

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:

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top