【问题标题】:Xcode 11: cannot instantiate view controller from storyboard with identifierXcode 11:无法从带有标识符的情节提要中实例化视图控制器
【发布时间】:2020-02-06 11:00:09
【问题描述】:

我使用的是 Xcode 11.1,我的部署目标是 iOS 10.0

我不能像以前那样实例化视图控制器。这是

中的代码

func 应用程序(_ 应用程序:UIApplication, didFinishLaunchingWithOptions 启动选项: [UIApplication.LaunchOptionsKey : Any]?) -> Bool

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(identifier: "TabBarController")

我收到错误消息:

'instantiateViewController(identifier:creator:)' 仅适用于 iOS 13.0 或更新版本

如何在 Xcode 11.1 上以编程方式从情节提要中实例化视图控制器。还有什么办法吗?

【问题讨论】:

  • 请查看您的Deployment target。我认为它低于 13.0,所以你不能使用这种方法。
  • 感谢@Mahendra。对于 13 以下的 iOS 版本,我通过将 identifier: 更改为 withIdentifier 解决了这个问题

标签: ios xcode11 swift5


【解决方案1】:

你需要使用

storyboard.instantiateViewController(withIdentifier: "TabBarController")

新的 instantiateViewController(identifier: "TabBarController") 仅在 iOS 13 上可用,并返回 ViewController 而不是 UIViewController,如您在此处看到的那样

【讨论】:

  • 谢谢你,withIdentifier:identifier: 非常棒
  • 感谢 Melian.. 原因??
  • 那么,你应该调用instantiateInitialViewController og iOS
  • @JesperSchläger 与该方法返回的内容有关,如果您使用的是 SwiftUI,则为 ViewController,如果您使用的是 UIKit,则为 UIViewController
  • 非常感谢。它节省了时间。 @Reinier,不错的收获...
【解决方案2】:

你应该做到以下几点:

let viewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")

参数应该是-withIdentifier-而不是-identifier-

【讨论】:

    【解决方案3】:
    //use instantiateViewController(withIdentifier:"") method to resolve notinstantiateViewController(Identifier:"") 
    
    
     let objRef : RatindAndReviewVC = self.storyboard?.instantiateViewController(withIdentifier: "RatindAndReviewVC") as! RatindAndReviewVC
    
           self.navigationController?.pushViewController(objRef, animated: true)
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多