在AppDelegate中使用SceneDelegate设置UIWindow

3

我试图在AppDelegate中以编程方式将var window:UIWindow?初始化为UIViewController,但我无法实现它。当我将Xcode更新到Version 11.0 (11A420a)时,在项目的init部分,我选择了使用Storyboard而不是SwiftUI,但我无法通过编程方式将UIWindowrootViewController分配给视图控制器。因此,首先我删除了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文件中尝试了init window但模拟器屏幕完全显示黑屏。那么我该怎么办?

提示:变量描述var window:UIWindow!不再添加到AppDelegate默认文件中,而是在SceneDelegate文件中。那么我能否从SceneDelegate文件设置我的rootViewController

如何以全编程的方式实现新的AppDelegate / SceneDelegate方法?


你需要学习并接受iOS 13的新场景架构。从Scenes文档开始。 - rmaddy
1
然后拿些爆米花,前往WWDC 2019视频,观看808、212、224、258和259号视频。 - rmaddy
@rmaddy 我会看这些,你有什么饮料推荐吗 :) ? - eemrah
1
对于任何寻找更新的“Single View App” Xcode模板的人,该模板支持iOS 12和13,并支持故事板或全代码用户界面,请参见https://github.com/rmaddy/XcodeTemplates。 - rmaddy
1个回答

1
AppDelegate 中,我删除了包含 UISceneSession 生命周期函数的行,然后它就可以工作了。
// 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.
    }

然后在 AppDelegate 中添加 var window: UIWindow? 属性,并初始化它并通过编程方式设置 rootViewController,然后进行操作。但是关于详情信息,我将会在了解关于 UIScene 工作流程后编辑我的答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接