如何使用xcode 11中的特定根控制器为新项目初始化uiwindow?

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

我想初始化appDelegate内部的窗口以显示特定的ViewController,这取决于某些情况。现在我有了这段代码:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow()

        let rootNavigationController = UIViewController()
        window?.rootViewController = rootNavigationController
        window?.rootViewController?.view.backgroundColor = .green

        window?.makeKeyAndVisible()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

}

我正在使用XCode11,并创建了新项目。我删除的SceneDelegate文件对此没有影响。还从Info.plist和部署信息

中删除了Main。

作为设备上的结果,我看到黑屏,但调试器向我显示了rootNavigationController应该是image from debugger

如何解决它或为XCode11实现此逻辑?

ios swift xcode appdelegate
1个回答
0
投票

解决方案:1)Inside manifest(plist) file remove Storyboard Name field2)在SceneDelegate.swift工具内部:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)

        let rootNavigationController = UIViewController()
        window?.rootViewController = rootNavigationController
        window?.rootViewController?.view.backgroundColor = .green
        window?.makeKeyAndVisible()
    }

并且它将适用于ios 13,如果您希望支持ios 12及更低版本,还需要在AppDelegate中实现此逻辑

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