使用SceneDelegate在AppDelegate中设置UIWindow

时间:2019-09-23 23:58:04

标签: ios swift

我试图在var window: UIWindow?中以编程方式将UIViewController初始化为AppDelegate。但是我无法实现。当我将Xcode更新为Version 11.0 (11A420a)时,在项目初始化部分中,我选择不使用Storyboard到SwiftUI,但是无法以编程方式将UIWindow's rootViewController分配给一个视图控制器。因此,首先,我删除了Main.storyboard文件,然后像往常一样将Info.plist挂起并删除了故事板部分,但如下所示,有一些关于UIApplicationSceneManifest的配置。还有

<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

顺便说一句,为了检查发生了什么,我尝试了SceneDelegate.swiftAppDelegate.swift文件中的初始化窗口,但是模拟器的屏幕完全显示黑屏。那我该怎么办?

提示:var window:UIWindow!变量说明不再添加到AppDelegate中的SceneDelegate默认值中。那么我可以从rootViewController文件中设置SceneDelegate吗?

采用新的AppDelegate/SceneDelegate方法以完全编程方式实现此目标的方式是什么?

1 个答案:

答案 0 :(得分:0)

AppDelegate中,我删除了包含UISceneSession Lifecyle函数的行,然后它起作用了。

// 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.
    }

比起将var window: UIWindow?属性添加到AppDelegate并以编程方式初始化并设置rootViewController,然后进行操作,但要获取详细信息,我将在得知{{ 1}}工作流程。