ios 如果应用程序委托要快速使用主故事板文件,则它必须实现 window 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29441682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:29:49  来源:igfitidea点击:

The app delegate must implement the window property if it wants to use a main storyboard file swift

iosiphonexcodeswiftsimulator

提问by Ethan Marcus

I have just developed an app, but when running in the simulator the debugger console says:

我刚刚开发了一个应用程序,但在模拟器中运行时,调试器控制台显示:

The app delegate must implement the window property if it wants to use a main storyboard file.

如果要使用主故事板文件,应用程序委托必须实现 window 属性。

I have an app delegate file. What does the message mean, and how can I get my app working?

我有一个应用程序委托文件。该消息是什么意思,我如何才能使我的应用程序正常运行?

回答by muneeb

Make sure you have the following property declaration in your AppDelegate class:

确保您的 AppDelegate 类中有以下属性声明:

var window: UIWindow?

回答by iMuzahid

If you run your project on earlier than iOS 13.0, in that case you will face the problem. Because of iOS 13 and later, app launch differently than earlier versions.

如果您在 iOS 13.0 之前运行您的项目,在这种情况下您将面临问题。由于 iOS 13 及更高版本,应用程序启动与早期版本不同。

  • In iOS 13 and later, use UISceneDelegateobjects to respond to life-cycle events in a scene-based app

  • In iOS 12 and earlier, use the UIApplicationDelegateobject to respond to life-cycle events.

  • 在 iOS 13 及更高版本中,UISceneDelegate在基于场景的应用程序中使用对象来响应生命周期事件

  • 在 iOS 12 及更早版本中,使用UIApplicationDelegate对象来响应生命周期事件。

When you launch the app in iOS 12 and earlier then UIApplicationMainclass expect a window property in your AppDelegateclass as like SceneDelegatehas. So your problem will be solved if you add the following line in your AppDelegateclass.

当您在 iOS 12 及更早版本中启动应用程序时,UIApplicationMain类期望您的AppDelegate类中SceneDelegate有一个 window 属性,就像这样。因此,如果您在AppDelegate类中添加以下行,您的问题将得到解决。

var window: UIWindow?

For Objective-C

对于 Objective-C

@property (strong, nonatomic) UIWindow *window;

You can find more here App's Life Cycle.

你可以在这里找到更多应用程序的生命周期

回答by TomTom

Just in case anyone comes across this again and is programming in Objective-C make sure you have this line of code in your AppDelegate.hfile:

以防万一有人再次遇到此问题并且正在使用 Objective-C 进行编程,请确保您的AppDelegate.h文件中有以下代码行:

@property (strong, nonatomic) UIWindow *window;

回答by Sravan

I have received this error, when I created new project in XCode 11. I have not used SwiftUI. Here are the steps, I have considered to fix this.

我在 XCode 11 中创建新项目时收到此错误。我没有使用SwiftUI. 这是步骤,我已经考虑解决这个问题。

  1. Deleted Application Scene Manifestentry from Info.plist
  2. Deleted SceneDelegate.swiftfile
  3. Deleted all scene related methods in AppDelegate.swiftclass
  4. added var window: UIWindow?property in AppDelegate.swiftclass
  1. 删除的Application Scene Manifest条目来自Info.plist
  2. 删除的SceneDelegate.swift文件
  3. 删除了AppDelegate.swift类中所有场景相关的方法
  4. var window: UIWindow?AppDelegate.swift类中添加属性

After these steps, I am able to run the app on version prior to iOS 13.

完成这些步骤后,我可以在 iOS 13 之前的版本上运行该应用程序。

[EDIT]
Finally, your AppDelegate.swiftfile should look something like the following.

[编辑]
最后,您的AppDelegate.swift文件应如下所示。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

}

回答by Yodagama

I had the same issue, just add var window: UIWindow?as the debug error says.

我遇到了同样的问题,只需var window: UIWindow?按照调试错误的说明添加即可。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

回答by Deepak Kumar

You can check your app delegate class:

您可以检查您的应用委托类:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    @available(iOS 13.0, *)
    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)
    }

    @available(iOS 13.0, *)
    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.
    }
}

回答by yoAlex5

Swift 5 & Xcode 11

斯威夫特 5 和 Xcode 11

Make sure that SceneDelegatecontains UIWindowproperty

确保SceneDelegate包含UIWindow属性

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    //...
}