[英]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!!!现在你准备好了!!!
Ok So i think here is something you can do if it suits:好的,所以我认为如果适合,您可以执行以下操作:
rootNavigationVC.swift
which is subclass of UINavigationController
rootNavigationVC.swift
类的文件对导航控制器进行类,它是UINavigationController
子类didLoad()
you can set it's viewController
or push one based on your conditiondidLoad()
您可以设置它的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.