在Swift中更改初始视图控制器

问题描述 投票:0回答:1

我希望在一个快速的类而不是情节提要中更改应用程序的初始视图控制器。运行时,代码返回一个Thread 1: signal SIGABRT,我知道这里有很多问题,但是我已经尝试了他们推荐的所有方法,但无法使其正常工作。大多数帖子都没有针对Swift 5的答案,因此我缺少在Swift版本中更改的内容吗?

这是我AppDelegate.swift中的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     self.window = UIWindow(frame: UIScreen.main.bounds)
     let storyboard = UIStoryboard(name: "Main", bundle: nil)
     let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")
     self.window?.rootViewController = initialViewController
     self.window?.makeKeyAndVisible()
     return true
}

这里是我的故事板ID为“ ViewController”的错误,但我收到的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x600000dcd8c0>) doesn't contain a view controller with identifier 'ViewController''

Xcode版本:11.2视图控制器名称:ViewController

任何帮助将不胜感激!

ios swift xcode viewcontroller appdelegate
1个回答
0
投票

进入情节提要,并确保您的ViewController具有类似的情节提要ID:

enter image description here

或者,如果它是情节提要的初始视图控制器,则可以启动视图控制器,如下所示:

let initialViewController = storyboard.instantiateInitialViewController() as? ViewController

使ViewController成为情节提要的初始视图控制器。单击您的视图控制器,并确保选中Is initial View Controller

enter image description here

然后您将在视图控制器旁边看到一个类似的箭头。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.