• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 setViewControllers 之后导航堆栈为零

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:48:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的层次结构:

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap