简体   繁体   English

使用 SceneDelegate 在 AppDelegate 中设置 UIWindow

[英]Set UIWindow in AppDelegate with SceneDelegate

I was trying to init var window: UIWindow?我试图初始化var window: UIWindow? to a UIViewController programmatically in AppDelegate .But I cannot achieve it.AppDelegate中以编程方式到UIViewController 。但我无法实现它。 When I was updated my Xcode to Version 11.0 (11A420a) , in project init section, I choosed to with Storyboard not to SwiftUI but I can not achieved to programmatically assign the UIWindow's rootViewController to a view controller. When I was updated my Xcode to Version 11.0 (11A420a) , in project init section, I choosed to with Storyboard not to SwiftUI but I can not achieved to programmatically assign the UIWindow's rootViewController to a view controller. So first of all, I removed the Main.storyboard file, then I hang with Info.plist and removed the storyboard section as usual like I do before but there's some configs about UIApplicationSceneManifest as shown below.所以首先,我删除了Main.storyboard文件,然后我挂起Info.plist并像以前一样像往常一样删除了 storyboard 部分,但是有一些关于UIApplicationSceneManifest的配置,如下所示。 And

<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>

By the way, for checking out what happens, I tried the init window in SceneDelegate.swift and AppDelegate.swift file but simulator's screen shows black screen fully.顺便说一句,为了检查发生了什么,我尝试了SceneDelegate.swiftAppDelegate.swift文件中的 init window 文件,但模拟器的屏幕完全显示为黑屏。 So what I must to do?那我必须做什么?

Hint: var window: UIWindow!提示:var window:UIWindow! variable description is not added to AppDelegate default anymore, its in SceneDelegate .变量描述不再添加到AppDelegate默认值中,它在SceneDelegate中。 So can I set my rootViewController from SceneDelegate file?那么我可以从SceneDelegate文件中设置我的rootViewController吗?

What's the way to achieve fully programmatically this in the new AppDelegate/SceneDelegate approach?在新的AppDelegate/SceneDelegate方法中完全以编程方式实现这一点的方法是什么?

In AppDelegate I removed the lines which contains functions of UISceneSession Lifecyle then it worked it.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.
    }

Than add var window: UIWindow?比添加var window: UIWindow? property to AppDelegate and inited it and set rootViewController programmatically, then worked it but for detail information, I will edit my answer as soon as I learned about UIScene workflow.属性到AppDelegate并初始化它并以编程方式设置rootViewController ,然后运行它,但有关详细信息,我将在了解UIScene工作流程后立即编辑我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM