简体   繁体   English

Xcode & Swift - 无法从 AppDelegate 实例化另一个视图 controller

[英]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.

相关问题 Swift-在AppDelegate中实例化视图控制器 - Swift - Instantiate View Controller in AppDelegate 如何从 appdelegate xcode 11 swift 5 呈现视图 controller - How to present a view controller from appdelegate xcode 11 swift 5 Swift 3、Xcode 8 实例化视图控制器不起作用 - Swift 3, Xcode 8 Instantiate View Controller is not working Swift –从AppDelegate向视图控制器添加子视图 - Swift – Add subview to View Controller from AppDelegate Swift:如何关闭从AppDelegate启动的视图控制器和导航控制器 - Swift: how to close view controller and navigation controller launched from AppDelegate 使用 Swift 在 Xcode 中避免从一个视图控制器到另一个视图控制器的延迟 - Avoiding Delay from One View Controller to Another in Xcode using Swift 无法实例化View Controller - Unable to Instantiate View Controller 从AppDelegate.swift将值分配给视图控制器 - Aassigning a value to a view controller from AppDelegate.swift 在Swift中从AppDelegate将CLBeacon的信息获取到View Controller - Getting CLBeacon's information from AppDelegate to View Controller in Swift 使用Swift从推送通知appdelegate更改视图控制器中UIWebView的URL - change URL of UIWebView in view controller from push notification appdelegate with Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM