[英]Xcode & Swift - unable to instantiate another view controller from AppDelegate
in this app the first time you use it you get 3 welcome pages.在这个应用程序中,当您第一次使用它时,您会获得 3 个欢迎页面。 After the last one I save a bool to true in UserDefaults in order to skip those welcome pages the next time the user launches the app.
在最后一个之后,我在 UserDefaults 中将 bool 保存为 true,以便在用户下次启动应用程序时跳过这些欢迎页面。
To do that, in AppDelegate.swift I do as following:为此,在 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
}
I of course added the storyboard ID to my view controller in the storyboard and I also checked with a breakpoint if the condition is met (and it's true).我当然在 storyboard 中将 storyboard ID 添加到我的视图 controller 中,并且我还检查了断点是否满足条件(这是真的)。
Despite this the Main Controller won't instantiate.尽管如此, Main Controller 不会实例化。
I made another app with this code and it's always worked!我用这段代码制作了另一个应用程序,它总是有效的!
Did I mistake something?我是不是弄错了什么?
For iOS 13+ you need to give the instantiateViewController(withIdentifier:)
in SceneDelegate, like this:对于 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")
}
}
}
Note: Your code would work for devices using iOS 13 below.注意:您的代码适用于使用以下 iOS 13 的设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.