【问题标题】:instantiateViewControllerWithIdentifier returning nil [closed]instanceViewControllerWithIdentifier 返回 nil [关闭]
【发布时间】:2016-07-11 12:48:17
【问题描述】:

我有授权窗口。当应用程序启动时它是好的。我点击登录并转到下一个窗口。但是当我点击退出并想要返回授权窗口时 - 我得到了零。在方法 signinView 控制器中为 nil。

【问题讨论】:

  • 请将您的minimal reproducible example 发布为标记为代码的文本,而不是图片。将其设为图片会阻止我们复制和粘贴代码,并削弱我们为您提供帮助的能力。
  • 你提供了大量的信息,我们不能仅仅假设你如何展示你的视图控制器。请贴代码
  • 最重要的是你不应该像这样访问故事板:self.window?.rootViewController!.storyboard?。这是我见过的最糟糕的方法。如果有任何内容为零,则任何一个应用程序都会崩溃(展开)

标签: ios swift appdelegate


【解决方案1】:

您需要从 Storyboard 中获取实例。

 let storyboard = UIStoryboard(name: "Main", bundle: nil)

let controller = storyboard.instantiateViewControllerWithIdentifier(“MyController”) 

通过这种方式,您将获得您的控制器实例。

确保您已经为此控制器设置了故事板 ID。

【讨论】:

  • 我在混合使用 IB 和以编程方式编码时遇到了这个问题。在一些纯代码VC中,我需要先声明storyboard,然后再实例化另一个VC,否则这个vc将无法打开。
【解决方案2】:

只需在函数的第二行替换下面的代码,希望它能解决您的问题。

let storyboard = UIStoryboard(name: "Main", bundle: nil)//Make it global
let controller = storyboard!.instantiateViewControllerWithIdentifier("your identifer")

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2019-06-20
    • 2017-12-16
    相关资源
    最近更新 更多