选择另一个选项卡时的 popToRootViewController [英] popToRootViewController when another tab is selected

查看:55
本文介绍了选择另一个选项卡时的 popToRootViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

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

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.

我正在尝试什么:

我正在设置 PostsShowViewController 一个 TabBarControllerDelegate 并且当一个选项卡项被选中时,我试图弹出到它的根视图控制器.然后,当用户回来时,他会看到 rootViewController,它是帖子列表,而不是 PostsShow 视图.

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.

代码:

viewDidAppearself.tabBarController.delegate = self;

viewDidDisappearself.tabBarController.delegate = nil;

标题UITabBarControllerDelegate

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

它是如何不起作用的:

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

按照答案建议的操作,我的行为稍微好一点,但最终还是出现错误.

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

代码

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

  1. 转到最近的标签
  2. 点击帖子进入 PostsShowview
  3. 转到热门标签
  4. 返回最近的标签
  5. 我看到了一个帖子列表(没有错误!)
  6. 返回热门标签:ERR_BAD_ACCESS!

这是我的故事板

全栈跟踪:

* 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

推荐答案

这是我在 Swift 中的做法:

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
}

类似于objective-C:

Similar in objective-C:

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

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

Notice that I used didSelectViewController method for UITabBarController.

您可以在此处查看:

这篇关于选择另一个选项卡时的 popToRootViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆