找不到 ‘window’ 在作用域内,场景代理

huangapple go评论70阅读模式
英文:

Couldn't find 'window' in scope, Scene delegate

问题

这个问题通常出现在 AppDelegate.swift 文件中,原因是由于新添加的 Scene 委托(Scene delegate),但是由于某种原因,我在 Scene 委托中遇到了 couldn't find window in scope 错误。

以下是你的代码:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // 使用这个方法来可选地配置并将 UIWindow `window` 附加到提供的 UIWindowScene `scene`。
    // 如果使用故事板(Storyboard),`window` 属性将自动初始化并附加到场景。
    // 这个委托不意味着连接的场景或会话是新的(请查看 `application:configurationForConnectingSceneSession` 替代)。
    guard let _ = (scene as? UIWindowScene) else { return }
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if (UserDefaults.standard.bool(forKey: "ISRemember")) {
        print(UserDefaults.standard.bool(forKey: "ISRemember"))
        let mainTabBarController = storyboard.instantiateViewController(identifier: "tabbar")
        window?.rootViewController = mainTabBarController
    }
    else {
        // 处理其他情况
    }
}

我尝试清理了 Xcode 的构建文件夹,但没有帮助。我还尝试重新启动了 Xcode,甚至我的 Mac,但仍然遇到相同的错误。有什么想法吗?

英文:

So this problem is mostly common in the AppDelegate.swift because of the newly added Scene delegate, but for some reason I get couldn't find window in scope in the scene delegate?

here is my code:

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

  if (UserDefaults.standard.bool(forKey: "ISRemember")) {
            print(UserDefaults.standard.bool(forKey: "ISRemember"))
            let mainTabBarController = storyboard.instantiateViewController(identifier: "tabbar")
            window?.rootViewController = mainTabBarController
        }
        else {
            
        }
    }

I tried cleaning xcode's build folder but it didn't help. I also tried restarting xcode and even my mac but I still get the same error. Any Ideas?

答案1

得分: 0

你的SceneDelegate类符合UIWindowSceneDelegate协议。该协议声明了一个名为window、类型为UIWindow的属性。

由于你的SceneDelegate类试图符合该协议,你必须提供这个window属性。

如果你基于Swift和Storyboard创建一个新的iOS应用项目,SceneDelegate类将已经包含window的必要声明。如果你的SceneDelegate类因某种原因没有这个属性,可以将以下代码添加到该类中:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow? // 添加这行

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
英文:

Your scene delegate class conforms to the protocol UIWindowSceneDelegate. The protocol declares that there is a property named window of type UIWindow.

Since your SceneDelegate class is trying to conform to that protocol, you must provide that window property.

If you create a new iOS app project based on Swift and Storyboard, the SceneDelegate class will already come with the necessary declaration of window. If your SceneDelegate class doesn't have this for some reason, add this property to the class:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow? // Add this line

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

huangapple
  • 本文由 发表于 2023年3月3日 19:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定