gpt4 book ai didi

ios - 选择另一个选项卡时的 popToRootViewController

转载 作者:可可西里 更新时间:2023-11-01 05:43:34 24 4
gpt4 key购买 nike

上下文:

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

我在尝试什么:

我正在设置 PostsShowViewController 一个 TabBarControllerDelegate 并且当一个选项卡项被选中时,我试图弹出到它的 Root View Controller 。然后,当用户回来时,他将看到 rootViewController,这是帖子列表而不是 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. 单击帖子以转到 PostsShow View
  3. 转到热门标签
  4. 返回到最近的选项卡(我希望看到的是帖子列表而不是 PostsShow View )
  5. 错误! EXC_BAD_ACCESS

编辑:按照答案建议的做法,我的行为稍微好一些,但仍然以错误告终。

代码

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
UINavigationController *navigation = (UINavigationController*) viewController;
[navigation popToRootViewControllerAnimated:NO];
}
  1. 转到最近的选项卡
  2. 点击帖子进入 PostsShowview
  3. 转到热门标签
  4. 返回最近的标签页
  5. 我看到了一个帖子列表(没有错误!)
  6. 返回常用标签:ERR_BAD_ACCESS!

编辑:这是我的 Storyboard enter image description here

编辑 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

最佳答案

这是我在 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 :

关于ios - 选择另一个选项卡时的 popToRootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com