草庐IT

ios - 尝试呈现其 View 的 UINavigationController

coder 2023-09-11 原文

应用委托(delegate)

   initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")as! UIViewController
    }

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

带我到 LoginViewController

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as? UIViewController

self.presentViewController(vc!, animated: true, completion: nil)

我点击登录按钮将我带到 CardsViewController

func goToProfile(button: UIBarButtonItem) {
   pageController.goToPreviousVC()

单击作为 UINavigationController 一部分的后退按钮会运行 goToProfile(上图)并将我带到 ViewController.swift,因为这是声明 pageController 的地方。

let pageController = ViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)

错误显示在这里

func goToPreviousVC() {
    //let currentControllers = self.navigationController?.viewControllers
   if viewControllers.isEmpty   {

    setViewControllers([profileVC], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)
    pageController.presentViewController(profileVC, animated: true, completion: nil)
 else {
    let previousVC = pageViewController(self,  viewControllerBeforeViewController: viewControllers[0] as! UIViewController)!
    setViewControllers([previousVC], direction:  UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: nil)
    }

错误:

Warning: Attempt to present <UINavigationController: 0x15ce314e0> on <MyApp.ViewController: 0x15cd2c6f0> whose view is not in the window hierarchy!

基本上流程就像这样 AppDelegate -> LoginViewController -> ViewController 我需要进入 ProfileViewController。

ProfileView 有 ProfileNavController 而 CardsView 有 CardsNavController

ViewController 有一个 pageController 的 storyboardID。

ProfileView 和 CardsView 都嵌入在 UINavigationControllers 中(因此有 NavController 扩展)。

我在全新安装后第二次运行该应用程序时,它运行良好(所有 Controller 都正常加载)。我应该在 AppDelegate 中推送 viewControllers 吗?

最佳答案

我已经使用 Xcode 7 检查了您的代码,这可能不是解决此问题的理想选择,因为我不得不将您的代码转换为 Swift 2.0,但这是我的发现。

问题

  1. 第一次打开应用,这个 block :

    if currentUser() != nil {
        initialViewController = pageController
    }
    else {
        initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
    }
    
    self.window?.rootViewController = initialViewController
    

将初始化 LoginViewController 并使其成为当前窗口的 rootViewController。 此时没有pageController初始化

  1. 当用户点击按钮转到个人资料屏幕时,将调用此方法

    func goToProfile(按钮: UIBarButtonItem) { pageController.goToPreviousVC()

此时,pageController 已初始化,当然,viewControllers 数组中没有任何内容。让我们看看 goToPreviousVC 方法中发生了什么:

原始方法如下所示:

    let nextVC = pageViewController(self, viewControllerAfterViewController: viewControllers[0] as UIViewController)!
    setViewControllers([nextVC], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

您可以明显看到的一件事是:调用 viewControllers[0] 可能会让您崩溃,因为 viewControllers 是一个空数组。 如果您使用 Swift 2.0,它甚至不允许您编译代码 :)

解决方案

让我们直接进入解决方案:确保 pageController 在尝试调用它的 viewControllers 之前可用。

我盲目地尝试在 Swift 2.0 中修复你的代码,发现这个方法对你有用:

之前:在 LoginViewController.swift 第 63 行

        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as? UIViewController

        self.presentViewController(vc!, animated: true, completion: nil)

之后:让我们像这样修复它

    let navc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CardsNavController") as! UINavigationController
    if let viewControllers = pageController.viewControllers where viewControllers.count == 0 {
        pageController.setViewControllers([navc.viewControllers[0]], direction: .Forward, animated: false, completion: nil)
    }
    self.presentViewController(pageController, animated: true, completion: nil)

它在这里运行良好,可能我不需要向您展示屏幕转换应该是什么样子:)

如果您也想要固定的源代码,请找到它 HERE .基本上,我将您的代码转换为 Swift 2.0,并忽略了不必要的部分,例如 Facebook 身份验证,以便更快地进行调查。

祝你好运,编码愉快!

关于ios - 尝试呈现其 View 的 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32594473/

有关ios - 尝试呈现其 View 的 UINavigationController的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  3. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  4. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. 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返回它复制的字节数,但是当我还没有下

  7. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

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

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

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. 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使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐