简体   繁体   English

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

[英]App delegate must implement the window property if it wants to use a main storyboard file

I've already tried all solutions I've been able to find in stackoverflow and none of them work.我已经尝试了我能够在 stackoverflow 中找到的所有解决方案,但都没有奏效。

This is my current AppDelegate.swift file, I think that I'm implementing the window property, some way or another:这是我当前的 AppDelegate.swift 文件,我想我正在以某种方式实现 window 属性:

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.
    window = UIWindow()
    window?.makeKeyAndVisible()
    let mainVC = UIViewController()
    window?.rootViewController = mainVC

    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.
    window = UIWindow()
          window?.makeKeyAndVisible()
          let mainVC = UIViewController()
          window?.rootViewController = mainVC
    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.
    window = UIWindow()
          window?.makeKeyAndVisible()
          let mainVC = UIViewController()
          window?.rootViewController = mainVC
}

} }

What can be happening so the warning still appears and my app keeps on showing a black screen?可能会发生什么情况导致警告仍然出现并且我的应用程序继续显示黑屏?

I guide you step by step.我一步一步指导你。

  1. 1st remove SceneDelegate file from the project. 1st 从项目中删除SceneDelegate文件。
  2. Add var window: UIWindow?添加var窗口:UIWindow? to AppDelegate.到 AppDelegate。
  3. Remove below func from AppDelegate.从 AppDelegate 中删除下面的 func。

    // MARK: UISceneSession Lifecycle // MARK: UISceneSession 生命周期

    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) } 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. }
  4. Remove Application Scene Manifest key from Info.plist file.Info.plist文件中删除应用程序场景清单键。


Extra额外的

Add below delegate methods in AppDelegate file, below didFinishLaunchingWithOptionsAppDelegate文件中添加以下委托方法,在didFinishLaunchingWithOptions下面

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

I just added var window: UIWindow?我刚刚添加了var window: UIWindow? into the AppDelegate class, the issue got fixed.进入 AppDelegate 类,问题得到解决。

Incase you are developing an Objective C app and face this error, follow same steps as mentioned above with only change in step 1. Add @property (nonatomic, retain) IBOutlet UIWindow window;如果您正在开发一个 Objective C 应用程序并遇到此错误,请按照与上述相同的步骤进行操作,仅在步骤 1 中进行更改。添加 @property (nonatomic, retain) IBOutlet UIWindow窗口; to AppDelegate.hAppDelegate.h

When you create a new project in Xcode 11.4 and above, you create a project directory structure with SceneDelegate files.当您在 Xcode 11.4 及更高版本中创建新项目时,您将使用 SceneDelegate 文件创建项目目录结构。

if you try to run a project you get an error.如果您尝试运行项目,则会出现错误。

Step 1 : Delete SceneDelegate.h & SceneDelegate.m第 1 步:删除 SceneDelegate.h 和 SceneDelegate.m

Step 2: In AppDelegate.h create window property.第 2 步:在 AppDelegate.h 中创建 window 属性。

@property (strong, nonatomic) UIWindow *window;

Step 3: In the main Storyboard set the storyboard id for ViewController.第 3 步:在主 Storyboard 中为 ViewController 设置 storyboard id。

在此处输入图片说明

and add a set window in didFinishLaunchingWithOptions method of appdelegate.并在 appdelegate 的 didFinishLaunchingWithOptions 方法中添加一个设置窗口。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

return YES;

For Swift: Use below code对于 Swift:使用以下代码

@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
}

} }

Note: Don't add window inside didFinishLaunchingWithOptions method for swift.注意:不要在 swift 的didFinishLaunchingWithOptions方法中添加窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM