繁体   English   中英

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

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

我已经尝试了我能够在 stackoverflow 中找到的所有解决方案,但都没有奏效。

这是我当前的 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
}

}

可能会发生什么情况导致警告仍然出现并且我的应用程序继续显示黑屏?

我一步一步指导你。

  1. 1st 从项目中删除SceneDelegate文件。
  2. 添加var窗口:UIWindow? 到 AppDelegate。
  3. 从 AppDelegate 中删除下面的 func。

    // 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. Info.plist文件中删除应用程序场景清单键。


额外的

AppDelegate文件中添加以下委托方法,在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:.
    }

我刚刚添加了var window: UIWindow? 进入 AppDelegate 类,问题得到解决。

如果您正在开发一个 Objective C 应用程序并遇到此错误,请按照与上述相同的步骤进行操作,仅在步骤 1 中进行更改。添加 @property (nonatomic, retain) IBOutlet UIWindow窗口; AppDelegate.h

当您在 Xcode 11.4 及更高版本中创建新项目时,您将使用 SceneDelegate 文件创建项目目录结构。

如果您尝试运行项目,则会出现错误。

第 1 步:删除 SceneDelegate.h 和 SceneDelegate.m

第 2 步:在 AppDelegate.h 中创建 window 属性。

@property (strong, nonatomic) UIWindow *window;

第 3 步:在主 Storyboard 中为 ViewController 设置 storyboard id。

在此处输入图片说明

并在 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;

对于 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
}

}

注意:不要在 swift 的didFinishLaunchingWithOptions方法中添加窗口。

暂无
暂无

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

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