草庐IT

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

coder 2024-01-24 原文

这是我的层次结构:

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 为零,而我已经从数组中设置了 viewControllers?

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

最佳答案

您是否检查过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];

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

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

有关ios - 导航堆栈在 setViewControllers 之后为零的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  5. ruby - 堆栈级别太深(SystemStackError) - 2

    我有Sinatra应用程序,需要测试我的应用程序。features/support/env.rb:require_relative"../../application"require"capybara"require"capybara/cucumber"require"rspec"WorlddoCapybara.app=ApplicationincludeCapybara::DSLincludeRSpec::Matchersendfeatures/one.feature:Feature:TesthomepageInordertomakesurepeoplecanopenmysiteIw

  6. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  7. ruby-on-rails - 在 Rails 3 中向 Active Admin 添加全局导航项的最佳方法是什么 - 2

    我正在尝试将全局导航菜单项添加到我的ActiveAdmin安装(在“仪表板”导航按钮旁边)。ActiveAdmin说这在他们的网站上是可能的,但他们没有任何关于如何实现它的文档。有谁知道如何做到这一点?编辑:抱歉,我应该更清楚。我想添加一个指向由任意文本/链接对组成的全局导航的链接。IE,如果我想添加一个链接到http://google.com在事件管理员的全局导航中使用文本“Google”,我将如何实现? 最佳答案 ActiveAdmin.register_page"Google"domenu:priority=>1,:label

  8. ruby-on-rails - Rails Asset Pipeline 更好的错误或堆栈跟踪 - 2

    刚刚将应用程序从rails3.0.9升级到3.2.1,当我运行bundleexecrakeassets:precompile时出现错误,这很好,但是回溯没有告诉我在哪里语法问题来self的css或scss文件。我尝试对“0ee5c0e69c92af0”进行greping,但该字符串没有出现在我的项目中。bundleexecrakeassets:precompile:allRAILS_ENV=productionRAILS_GROUPS=assets--trace**Invokeassets:precompile:all(first_time)**Executeassets:precom

  9. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  10. python - 保存堆栈? - 2

    我只是好奇,是否可以将程序的所有变量和当前状态转储到一个文件中,然后在另一台计算机上恢复它?!假设我有一个用Python或Ruby编写的小程序,给定特定条件,它会将所有当前变量和当前状态转储到一个文件中。稍后,我可以在另一台机器上再次加载它,然后返回它。类似VM快照功能。我在这里看到过这样的问题,但与Java相关,保存当前的JVM并在不同的JVM中再次运行它。大多数人都说没有那样的东西,只有Terracotta有一些东西,仍然不完美。谢谢。阐明我要实现的目标:给定2个或更多RaspberryPi,我试图在Pinº1上运行我的软件,但是当我需要用它做一些不同的事情时,我需要将软件移动到P

随机推荐