Ios 选择其他选项卡时的popToRootViewController
上下文: 我同时使用TabViewController和NavigationController。这两个选项卡是Ios 选择其他选项卡时的popToRootViewController,ios,objective-c,Ios,Objective C,上下文: 我同时使用TabViewController和NavigationController。这两个选项卡是RECENT和POPULAR,它们显示帖子列表。想象你在RECENT选项卡中,点击一篇文章,进入postsShow视图。所以你是导航堆栈中更深的一个。当您转到POPULAR选项卡并返回到RECENT选项卡时,您仍然可以看到以前单击的帖子。但是我想显示一个帖子列表 我正在尝试的: 我正在设置PostsShowViewControlleraTabBarControllerDelegate,
RECENT
和POPULAR
,它们显示帖子列表。想象你在RECENT
选项卡中,点击一篇文章,进入postsShow
视图。所以你是导航堆栈中更深的一个。当您转到POPULAR
选项卡并返回到RECENT
选项卡时,您仍然可以看到以前单击的帖子。但是我想显示一个帖子列表
我正在尝试的:
我正在设置PostsShowViewController
aTabBarControllerDelegate
,当选择选项卡项时,我尝试弹出到其根视图控制器。然后,当用户返回时,他将看到rootViewController,这是帖子列表,而不是PostsShow视图
代码:
视图显示
self.tabBarController.delegate=self代码>
看不见
self.tabBarController.delegate=nil代码>
标题
UITabBarControllerDelegate
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
[self.navigationController popToRootViewControllerAnimated:NO];
return YES;
}
它的工作原理:
转到“最近”选项卡
单击帖子以转到帖子展示视图
转到“常用”选项卡
回到“最近”选项卡(我希望看到的是帖子列表,而不是PostsShow视图)
错误<代码>EXC\U坏访问
编辑:
按照答案的建议,我的行为会稍微好一点,但最终还是会出错
代码
转到“最近”选项卡
单击“发布”转到“发布显示”视图
转到“常用”选项卡
返回“最近”选项卡
我看到一个帖子列表(没有错误!)
返回常用选项卡:错误错误错误访问李>
编辑:
这是我的故事板
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
试试这个:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if ([viewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *)viewController;
[navController popToRootViewControllerAnimated:NO];
}
}
以下是我在swift中的做法:
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
self.tabBarSelectedIndex = tabBarController.selectedIndex
var navigation = viewController as! UINavigationController
navigation.popToRootViewControllerAnimated(false)
// rest of the logic
}
与目标C类似:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
self.tabBarSelectedIndex = tabBarController.selectedIndex;
UINavigationController *navigation = (UINavigationController*) viewController;
[navigation popToRootViewControllerAnimated:NO];
}
请注意,我对UITabBarController使用了didSelectViewController方法
您可以检查它:您的层次结构是什么?TabBarViewController->NavigationController?@NickCatib是。每个选项卡项都连接到一个NavigationController。请参阅此链接可能会有所帮助。您不应该将viewController强制转换为UINavigationController,然后将其弹出吗?我尝试过这样做(按照@coder1010的回答所述),结果如下:1。转到最近的选项卡2。单击post转到PostsShowview 3。转到常用选项卡4。返回最近的选项卡5。我看到一个帖子列表(没有错误!)6。返回常用选项卡:错误错误错误访问!谢谢你的回答。请查看我的更新问题。可能是您在“常用”选项卡上没有navigationcontroller。如果([viewController isKindOfClass:[UINavigationController class]])我确实这样做了(请参见更新的问题),请尝试添加以下条件,如我在回答中所述,并且我也尝试添加条件:(感谢您的回答。请查看我的更新问题。这可能是因为您在其中一个选项卡上有UINavigationController,而在另一个选项卡上没有UINavigationController。您只能对某些selectedIndex执行此弹出操作,即如果(tabBarController.selectedIndex==0)强制转换为导航和弹出。此外,您可以启用异常断点以确定其崩溃的位置。它发生在UIApplicationMain
。所有选项卡都连接到它们自己的导航控制器。是的,因为它是实际捕获它的主线程。如果您尝试此操作:您将在tabbar中准确地看到它失败的地方。A此外,在崩溃发生后,您可以在控制台中写入“bt”,以便查看完整的堆栈跟踪
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
self.tabBarSelectedIndex = tabBarController.selectedIndex;
UINavigationController *navigation = (UINavigationController*) viewController;
[navigation popToRootViewControllerAnimated:NO];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
}
}