简体   繁体   中英

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.

标签栏图片 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM