【问题标题】:Is 'window' not an identifier in Swift iOS Development?'window' 不是 Swift iOS 开发中的标识符吗?
【发布时间】:2019-06-15 14:26:44
【问题描述】:

我正在创建一个 iOS 应用程序。我遇到了一个问题:标识符“窗口”未解析。

这可能是因为我使用的是 Swift 的测试版和 Xcode 的测试版,但我需要它们来开发这个应用程序。我尝试搜索其他问题,但没有关于标识符“window”的答案。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    guard let splitViewController = window?.rootViewController as? UISplitViewController,
        let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
        let masterViewController = leftNavController.topViewController as? MasterViewController,
        let detailViewController = splitViewController.viewControllers.last as? DetailViewController
        else { fatalError() }

    let firstMonster = masterViewController.monsters.first
    detailViewController.monster = firstMonster

    return true
}

我希望最终能够选择一个菜单项并在另一个视图控制器上显示详细信息,但是我收到了一个带有未解析标识符的错误。

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    这可能是因为我使用的是 Swift 的测试版和 Xcode 的测试版

    在 iOS 13 中,因此在 Xcode 11 下创建的任何新项目中,window 属性已从应用程序委托中移出,现在驻留在 场景委托中。

    https://developer.apple.com/documentation/uikit/uiscenedelegate

    window 属性不是 UISceneDelegate 协议的正式组成部分,但它是 UIWindowSceneDelegate 协议的一部分:

    https://developer.apple.com/documentation/uikit/uiwindowscenedelegate

    在新架构中,窗口场景委托实例是界面创建和维护的场所,包括对窗口的访问、对其rootViewController的配置等。项目模板现在包含一个文件 SceneDelegate.swift,其中包含符合 UIWindowSceneDelegate 的 SceneDelegate 类的声明;这是样板代码的结构:

    import UIKit
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
        var window: UIWindow?
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let _ = (scene as? UIWindowScene) else { return }
        }
        func sceneDidDisconnect(_ scene: UIScene) {
        }
        func sceneDidBecomeActive(_ scene: UIScene) {
        }
        func sceneWillResignActive(_ scene: UIScene) {
        }
        func sceneWillEnterForeground(_ scene: UIScene) {
        }
        func sceneDidEnterBackground(_ scene: UIScene) {
        }
    }
    

    因此应将您的界面创建代码移至场景委托的scene(_:willConnectTo:options:) 实现中。

    https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene

    您还需要将任何ActiveForeground/Background 代码移出应用委托并移入场景委托。您应该在 iOS 13 下观看多窗口/多任务处理的 WWDC 2019 视频,同时阅读文档:

    https://developer.apple.com/documentation/uikit/app_and_scenes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-03
      • 2016-05-25
      • 2014-03-15
      • 1970-01-01
      • 2016-06-27
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多