OGeek|极客世界-中国程序员成长平台

标题: ios - 在 setViewControllers 之后导航堆栈为零 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 00:48
标题: ios - 在 setViewControllers 之后导航堆栈为零

这是我的层次结构:

A - B - C - D (tabs)
|   |   |   |
|   |   |   |
----------------> AddEventViewController (can be called from any tabs)
    |
    |
    |----> SuggestFeedController (can be accessed from B only)

假设我当前在 D 选项卡中,我调用了 AddEventViewController。 在此之后,我想将用户重定向到“SuggestFeedController”,但我想保持层次结构,这意味着按下后退按钮应该重定向到 B 而不是 AddEventViewController 也不是 D。

我尝试做的事情如下:

UITabBarController *tabbarController = ((UITabBarController *)appDelegate.window.rootViewController);
                 [tabbarController setSelectedIndex:1]; // Selecting the B tab

UIStoryboard* storyboard=  appDelegate.window.rootViewController.storyboard;

UICategoryFeedController *cfvC = [storyboard instantiateViewControllerWithIdentifier"UICategoryFeedController"]; // Instantiating B view Controller

SuggestFeed *suggestFeed = [storyboard instantiateViewControllerWithIdentifier"SuggestFeed"];


NSArray *viewControllers = [[NSArray alloc] initWithObjects:cfvC, suggestFeed, nil]; // Trying to create my own navigation stack, B first, then Suggest Feed


[self.navigationController pushViewController:suggestFeed animated:NO]; // Some guys on internet do the push, even if I don't think it's necessary because it's already in the stack


NSLog(@"nav stack1 : %@", self.navigationController.viewControllers );
/* returns stack1 : (
    "<D: 0xa627990>",
    "<AddEventViewController: 0x108cb7e0>",
    "<SuggestFeed: 0x123735f0>"
) */


[self.navigationController setViewControllers:viewControllers animated:NO];

NSLog(@"nav stack2 : %@", self.navigationController.viewControllers );
/* returns stack2 : (null) */

我做错了吗? (愚蠢的问题)

为什么 stack2 是 nil,而我已经从我的数组中设置了 viewControllers?

如果你能带来一些帮助,在此先感谢:-)



Best Answer-推荐答案


您是否检查过 cfvC 不是 nil?如果 @"UICategoryFeedController" 与 Storyboard 中的标识符不匹配,则可能是这种情况。我问是因为这会使 viewControllers 成为一个空数组,这将解释您的日志记录结果。

另一方面,我猜你有一个设置,其中 UITabBarController 中的每个选项卡都包含自己的导航堆栈(即有四个 UINavigationController 实例,设置为标签栏 Controller 的 viewControllers)。其次,我假设您的示例中的 self 是 View Controller D。这意味着每当你在这个方法中调用 self.navigationController 时,你得到的是标签 D 中的导航 Controller ,而不管选择了哪个标签。要在选项卡 B 中设置 View Controller ,您必须在选项卡 B 中获取对导航 Controller 的引用。如果我的假设是正确的,您可以执行以下操作:

UINavigationController *tabBNavigationController = tabbarController.viewControllers[1];

this 导航 Controller 上设置您的 viewControllers 数组应该可以解决问题。另外,你是对的,你不想做 pushViewController 位。

关于ios - 在 setViewControllers 之后导航堆栈为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362698/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4