2

我正在使用多个窗口设置我的应用程序。它运作良好。但是当我从跳板打开我的应用程序时,它每次都会创建一个新窗口。

我正在使用最新的 Xcode 和 iPadOS 13.0 测试版。我所有的视图控制器、视图等都是以编程方式制作的。我唯一的故事板是 LaunchScreen。

信息列表

<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default</string>
                    <key>UISceneDelegateClassName</key>
                    <string>ComicReader.SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

AppDelegate.swift

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    UISceneConfiguration(name: "Default", sessionRole: connectingSceneSession.role)
}

SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else { return }

        window = UIWindow(windowScene: scene)
        window?.rootViewController = DelegateHelper.rootViewController()
        window?.makeKeyAndVisible()
    }
}

在Apple的Gallery示例中,如果打开应用程序,滑动到主屏幕,然后再次打开应用程序,我就回到了原来的位置,没有scene(_:willConnectTo)再次调用。在我自己的应用程序上,scene(_:willConnectTo)每次打开应用程序时都会调用它,并且放置断点向我表明,我确实在每次启动时收到了不同的 UIScene 和 UISceneSession 对象。

我没有显示任何 NSUserActivity 代码,因为我首先是因为我还没有任何状态恢复。实施它不会改变任何事情。

如果你有一些想法,我很高兴读到你!

4

1 回答 1

3

所以,我从上周开始就在寻找这个。今天,我决定评论我所有的 AppDelegate、SceneDelegate,并在 Info.plist 中只保留一个场景配置。从默认模板重写 AppDelegate 和 SceneDelegate 以逐步挖掘。

它适用于第一次尝试使用默认模板。我重写了所有相同的东西……仍然有效。

问题?UIWindowSceneSessionRoleApplication 数组中 Info.plist 中的“默认”配置是“项目 1”,而不是“项目 0”。git stash 一切,只有重新排序才能使它工作。

我希望这可以帮助别人。

于 2019-08-31T12:42:51.537 回答