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

我们两清 提交于 2020-02-12 08:22:18

问题


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.

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


回答1:


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

var window: UIWindow?



回答2:


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.

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

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

When you launch the app in iOS 12 and earlier then UIApplicationMain class expect a window property in your AppDelegate class as like SceneDelegate has. So your problem will be solved if you add the following line in your AppDelegate class.

var window: UIWindow?

For Objective-C

@property (strong, nonatomic) UIWindow *window;

You can find more here App's Life Cycle.




回答3:


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.h file:

@property (strong, nonatomic) UIWindow *window;



回答4:


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.

  1. Deleted Application Scene Manifest entry from Info.plist
  2. Deleted SceneDelegate.swift file
  3. Deleted all scene related methods in AppDelegate.swift class
  4. added var window: UIWindow? property in AppDelegate.swift class

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

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

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
    }

}


来源:https://stackoverflow.com/questions/29441682/the-app-delegate-must-implement-the-window-property-if-it-wants-to-use-a-main-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!