user1838169
user1838169

Reputation: 534

iOS refresh tab bar items after selected

I have a tab bar that can browse into sub screens.

Restart the tab bar item (first screen) when you select the tab item.

Tab bar picture So basically when you select the first tab bar(see pic above) and select something from the tableview you are directed to the collection view (black screen). When you select the second tab bar item and go back to the first item in the tab bar, it continues where it left off (black screen).

How do i make it start over?

I've tried using this,

- (void) viewDidLoad{
    [self.tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:@"changedTabbarIndex"]; }


- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void
*)context{
    [self viewDidAppear:YES]; }

But that only reloads the screen on the second click.

Upvotes: 1

Views: 4518

Answers (1)

danypata
danypata

Reputation: 10175

From what I see in your image you have UINavigationController as root view controller for each tab bar item, so what you can do is in your UITabBarController implement the following method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController  {
  if (tabBarController.selectedIndex == YOUR_TAB_INDEX) { 
        //YOUR_TAB_INDEX is the index of the tab bar item for which you want to show the rootView controller
    UINavigationController *navController = (UINavigationController*)viewController;
    [navController popToRootViewControllerAnimated:YES]
   }

}

This will remove all the view controllers that were added to the UINavigationController which is the root view controller of your tab bar item

Upvotes: 3

Related Questions