[英]Xcode & Swift - unable to instantiate another view controller from AppDelegate
在这个应用程序中,当您第一次使用它时,您会获得 3 个欢迎页面。 在最后一个之后,我在 UserDefaults 中将 bool 保存为 true,以便在用户下次启动应用程序时跳过这些欢迎页面。
为此,在 AppDelegate.swift 中,我执行以下操作:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if UserDefaultsVault.shared.getDidFinishIntro() == true {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyboard.instantiateViewController(withIdentifier: "mainPage") as UIViewController
self.window?.rootViewController = mainVC
self.window?.makeKeyAndVisible()
}
return true
}
我当然在 storyboard 中将 storyboard ID 添加到我的视图 controller 中,并且我还检查了断点是否满足条件(这是真的)。
尽管如此, Main Controller 不会实例化。
我用这段代码制作了另一个应用程序,它总是有效的!
我是不是弄错了什么?
对于 iOS 13+,您需要在 SceneDelegate 中提供instantiateViewController(withIdentifier:)
,如下所示:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
if UserDefaultsVault.shared.getDidFinishIntro() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "mainPage")
}
}
}
注意:您的代码适用于使用以下 iOS 13 的设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.