简体   繁体   English

无法实例化视图控制器。 - 斯威夫特

[英]Failed to instantiate view controller. - Swift

So, I'm trying to instantiate my view controller programmatically using storyboard references.所以,我正在尝试使用故事板引用以编程方式实例化我的视图控制器。

I've put this code in the AppDelegate:我已将此代码放在 AppDelegate 中:

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialController: WelcomePageViewController = mainStoryboard.instantiateViewController(withIdentifier: "WelcomePageViewController") as! UITabBarController
        window?.rootViewController = initialController        
    window?.makeKeyAndVisible()
    return true
}

And set this inside my view controller's storyboard:并将其设置在我的视图控制器的故事板中:

在此处输入图片说明

However when I run the application, only a black screen is shown and this message: "Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?"但是,当我运行该应用程序时,只显示黑屏和此消息:“无法实例化 UIMainStoryboardFile 'Main' 的默认视图控制器 - 可能未设置指定的入口点?”

I've used this exact code in other apps and it works just fine.我在其他应用程序中使用了这个确切的代码,它工作得很好。

I've tried to clean build folder, to run it on an actual device and to create and instantiate a different storyboard file but nothing worked.我试图清理构建文件夹,在实际设备上运行它并创建和实例化不同的故事板文件,但没有任何效果。

Open your Main.storyboard file and find WelcomePageViewController .打开您的Main.storyboard文件并找到WelcomePageViewController When it's selected, go to the attributes inspector and check the box marked Is Initial View Controller .选中后,转到属性检查器并选中标记为Is Initial View Controller的框。

You should see a right-facing arrow appear to the left of WelcomePageViewController , showing that it's your storyboard's entry point.您应该会看到WelcomePageViewController左侧出现一个向右箭头,表明它是故事板的入口点。

Now you're all set to go!!!现在你准备好了!!!

这就是<code>是初始视图控制器</code>的样子

Ok So i think here is something you can do if it suits:好的,所以我认为如果适合,您可以执行以下操作:

  • set entry point to the navigation controller设置导航控制器的入口点
  • class that navigation controller with a file like rootNavigationVC.swift which is subclass of UINavigationController使用rootNavigationVC.swift类的文件对导航控制器进行类,它是UINavigationController子类
  • now in didLoad() you can set it's viewController or push one based on your condition现在在didLoad()您可以设置它的viewController或根据您的条件推送一个

trust me its better than coding in AppDelegate.swift which never worked for me.相信我,它比在AppDelegate.swift中编码更好,后者对我来说从来没有用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Swift) 表视图 Controller。 未触发 cellForRowAt - (Swift) Table View Controller. cellForRowAt is not triggered Swift-在AppDelegate中实例化视图控制器 - Swift - Instantiate View Controller in AppDelegate 从另一个视图控制器访问变量。 迅速 - Access a variable from another view controller. Swift 实例化View Controller Swift 3 Tab Bar Controller - Instantiate View Controller Swift 3 Tab Bar Controller Swift /如何使用根视图控制器/页面视图控制器隐藏NavigationBar。 屏幕上方的小缝隙 - Swift / How to hide NavigationBar with Root View Controller / Page View Controller. Little gap on top of screen 在Objective C中实例化Swift View Controller - Instantiate Swift View Controller in Objective C 无法实例化视图控制器,Swift,iOS - can not instantiate a view controller, swift, ios 无法实例化UIMainStoryboardFile&#39;Main&#39;的默认视图控制器 - Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' 解析中的交叉引用列决定要在视图控制器上显示哪些用户。 快速的用户搜索设置 - Cross reference columns in Parse to decide which users to Display on a view controller. Swift, user search settings 如何在转到另一个视图控制器时保持选项卡栏。 迅速 - how to keep tab bar while go to another view controller. Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM