Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

popToRootViewController when another tab is selected

Context:

I am using TabViewController and NavigationController at the same time. The two tabs are RECENT and POPULAR and they show a list of posts. Imagine you're inside RECENT tab and click a post, and you go into a postsShow view. So you're one deeper in a navigation stack. When you go to POPULAR tab and come back to RECENT tab, you are still seeing the post you clicked before. But I want to show a list of posts instead.

What I am trying:

I am setting PostsShowViewController a TabBarControllerDelegate and when a tab item is selected, I am trying to pop to its root view controller. Then, when the user comes back, he will see the rootViewController, which is the list of posts instead of PostsShow view.

Code:

viewDidAppear self.tabBarController.delegate = self;

viewDidDisappear self.tabBarController.delegate = nil;

header UITabBarControllerDelegate

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    [self.navigationController popToRootViewControllerAnimated:NO];
    return YES;
}

How it doesn't work:

  1. Go to recent tab
  2. Click a post to go to PostsShow view
  3. Go to popular tab
  4. Go back to recent tab (I am expecting to see a list of posts instead of PostsShow view)
  5. Error! EXC_BAD_ACCESS

EDIT: Following what the answers suggest doing, I get a slightly better behavior but still end up with an error.

Code

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UINavigationController *navigation = (UINavigationController*) viewController;
    [navigation popToRootViewControllerAnimated:NO];
}
  1. go to recent tab
  2. click post to go to PostsShowview
  3. go to popular tab
  4. go back to recent tab
  5. I see a list of posts (no error!)
  6. go back to popular tab : ERR_BAD_ACCESS!

EDIT: this is my storyboard enter image description here

EDIT2:

full stack track:

* thread #1: tid = 0x4a37c, 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
    frame #0: 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16
    frame #1: 0x000000018ab52078 UIKit`-[UITabBarController _tabBarItemClicked:] + 104
    frame #2: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
    frame #3: 0x000000018ab51fb4 UIKit`-[UITabBar _sendAction:withEvent:] + 468
    frame #4: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
    frame #5: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612
    frame #6: 0x000000018ab51bec UIKit`-[UITabBar(Static) _buttonUp:] + 128
    frame #7: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
    frame #8: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612
    frame #9: 0x000000018a988b88 UIKit`-[UIControl touchesEnded:withEvent:] + 592
    frame #10: 0x000000018a947da8 UIKit`_UIGestureRecognizerUpdate + 8536
    frame #11: 0x0000000185e8fff0 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
    frame #12: 0x0000000185e8cf7c CoreFoundation`__CFRunLoopDoObservers + 360
    frame #13: 0x0000000185e8d35c CoreFoundation`__CFRunLoopRun + 836
    frame #14: 0x0000000185db8f74 CoreFoundation`CFRunLoopRunSpecific + 396
    frame #15: 0x000000018f8136fc GraphicsServices`GSEventRunModal + 168
    frame #16: 0x000000018a9bad94 UIKit`UIApplicationMain + 1488
  * frame #17: 0x0000000100023ff4 toaster-objc`main(argc=1, argv=0x000000016fdeba50) + 124 at main.m:14
    frame #18: 0x000000019824ea08 libdyld.dylib`start + 4
like image 777
Maximus S Avatar asked Sep 27 '22 19:09

Maximus S


1 Answers

Here is how I did it in swift:

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

    self.tabBarSelectedIndex = tabBarController.selectedIndex
    var navigation = viewController as! UINavigationController
    navigation.popToRootViewControllerAnimated(false)
    // rest of the logic
}

Similar in objective-C:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    self.tabBarSelectedIndex = tabBarController.selectedIndex;
    UINavigationController *navigation = (UINavigationController*) viewController;
    [navigation popToRootViewControllerAnimated:NO];
}

Notice that I used didSelectViewController method for UITabBarController.

You can check it here:

like image 157
Miknash Avatar answered Sep 30 '22 06:09

Miknash