选择另一个选项卡时的popToRootViewController

时间:2015-09-07 12:39:03

标签: ios objective-c

上下文

我同时使用TabViewController和NavigationController。这两个标签是RECENTPOPULAR,它们会显示帖子列表。想象一下,您在RECENT标签内,点击了一个帖子,然后进入postsShow视图。所以你在导航堆栈中更深。当您转到POPULAR标签并返回RECENT标签时,您仍然会看到之前点击过的帖子。但我想显示一个帖子列表。

我在尝试什么:

我正在设置PostsShowViewController一个TabBarControllerDelegate,当选择一个标签项时,我正在尝试弹出其根视图控制器。然后,当用户回来时,他将看到rootViewController,它是帖子列表而不是PostsShow视图。

代码:

viewDidAppear self.tabBarController.delegate = self;

viewDidDisappear self.tabBarController.delegate = nil;

UITabBarControllerDelegate

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

如何无效

  1. 转到最近的标签
  2. 点击帖子转到PostsShow视图
  3. 转到热门标签
  4. 返回最近的标签页(我希望看到帖子列表而不是PostsShow视图)
  5. 错误! EXC_BAD_ACCESS
  6. 修改 根据答案建议的做法,我得到了稍微好一点的行为,但最终仍然出现错误。

    代码

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        UINavigationController *navigation = (UINavigationController*) viewController;
        [navigation popToRootViewControllerAnimated:NO];
    }
    
    1. 转到最近的标签
    2. 点击帖子转到PostsShowview
    3. 转到热门标签
    4. 返回最近的标签
    5. 我看到一个帖子列表(没有错误!)
    6. 返回热门标签:ERR_BAD_ACCESS!
    7. 修改 这是我的故事板 enter image description here

      EDIT2:

      完整筹码追踪:

      * 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
      

3 个答案:

答案 0 :(得分:3)

以下是我在swift中的表现:

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

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

类似于Objective-C:

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

请注意,我为UITabBarController使用了didSelectViewController方法。

您可以查看here

答案 1 :(得分:0)

试试这个:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if ([viewController isKindOfClass:[UINavigationController class]])
{
    UINavigationController *navController = (UINavigationController *)viewController;

    [navController popToRootViewControllerAnimated:NO];

}
}

答案 2 :(得分:0)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
    }
}
相关问题