选择另一个选项卡时的 popToRootViewController

Max*_*s S 5 objective-c ios

语境:

我同时使用 TabViewController 和 NavigationController。这两个选项卡是RECENTPOPULAR,它们显示帖子列表。想象一下,您在RECENT选项卡内单击帖子,然后进入postsShow视图。因此,您在导航堆栈中处于更深的位置。当您转到POPULAR选项卡并返回RECENT选项卡时,您仍会看到之前单击的帖子。但我想显示一个帖子列表。

我在尝试什么:

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

代码:

视图DidAppear self.tabBarController.delegate = self;

视图消失了 self.tabBarController.delegate = nil;

标题 UITabBarControllerDelegate

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

它如何不起作用:

  1. 转到最近选项卡
  2. 单击帖子转到帖子显示视图
  3. 转到热门标签
  4. 返回最近的选项卡(我希望看到帖子列表而不是 PostsShow 视图)
  5. 错误! EXC_BAD_ACCESS

编辑: 按照答案建议做的事情,我的行为稍微好一点,但最终还是出现错误。

代码

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UINavigationController *navigation = (UINavigationController*) viewController;
    [navigation popToRootViewControllerAnimated:NO];
}
Run Code Online (Sandbox Code Playgroud)
  1. 转到最近的标签
  2. 点击帖子进入 PostsShowview
  3. 转到流行标签
  4. 返回最近的标签
  5. 我看到一个帖子列表(没有错误!)
  6. 返回流行标签:ERR_BAD_ACCESS!

编辑: 这是我的故事板 在此处输入图片说明

编辑2:

全栈轨道:

* 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
Run Code Online (Sandbox Code Playgroud)

Mik*_*ash 4

这是我快速完成的方法:

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

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

Objective-C 中类似:

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

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

您可以在这里查看: