【问题标题】:instantiateViewController: trying to open new viewinstantiateViewController:试图打开新视图
【发布时间】:2016-11-24 11:29:52
【问题描述】:

我正在尝试打开新视图

ViewController.swift -> InAppBrowserController.swift

Main.storyboard -> InAppBrowser View make & Storyboard Id -> 好的。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyboard.instantiateViewController(withIdentifier: "InAppBrowserView") as! InAppBrowserController
self.present(nextViewController, animated: true, completion: nil)

块引用 致命错误:在展开可选值时意外发现 nil 2016-11-24 20:24:50.569000 GMK SWork[5920:2148780] 致命错误:在展开可选值时意外发现 nil

有什么问题?

【问题讨论】:

    标签: ios swift3 ios10


    【解决方案1】:

    在你的故事板中,你必须设置 UIViewController 的标识符,然后才能以这种方式获得:

    let yourController = UIStoryboard.init(name: @"yourStoryboardName", bundle: nil).instantiateViewController(withIdentifier: "yourVCIdentifier") as? UIViewController
    

    设置标识符很重要(我认为你的控制器没有找到)

    在任何情况下,如果您没有特殊需求,您应该直接从情节提要中使用“segue”并推送/展示您的控制器:

    self.performSegue(withIdentifier: "yourVCIdentifier", sender: self)
    

    【讨论】:

    • 我已经尝试过了,但它对我帮助很大。谢谢!~
    【解决方案2】:
     let storyBoard = UIStoryboard(name: "Main", bundle: nil)
     if let controller = storyBoard.instantiateViewController(withIdentifier: "InAppBrowserView") as? InAppBrowserController {  
        self.present(controller, animated: true, completion: nil)
        } else {
            print("Controller not found")
        }
    

    如上编写安全代码,并确保已在情节提要中添加标识符并将类正确分配给视图控制器,否则它将返回 nil。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多