如何使用 Swift 在 AppDelegate 中使用旧方法设置 rootViewController

How to use old method of setting rootViewController in AppDelegate using Swift

我刚刚开始了 iOS 的新项目。我使用 xcode 11 和 iOS 13 创建了项目。当我创建项目时,我发现为了设置我们自己的 rootController,我们必须使用 sceneDelegate 而不是 AppDelegate。我想问一下是否有可能使用旧方法在 AppDelegate 中设置 rootControllers 而不是使用 sceneDelegate。

 import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let loginController: HomeViewController = HomeViewController(nibName: "HomeViewController", bundle: nil)

       //        let navController: UINavigationController = UINavigationController(rootViewController: loginController)
               self.window?.rootViewController = loginController
               self.window?.makeKeyAndVisible()
        // Override point for customization after application launch.
        return true
    }
}

按照这些步骤使用 AppDelegate 并选择退出 SceneDelegate

  1. 转到 Info.plist 并从 Info.plist.
  2. 中删除 Application Scene Manifest 条目
  3. 移除 SceneDelegate.swift
  4. 在您的 AppDelegate.swift 文件中添加 var window: UIWindow?
  5. 从AppDelegate.swift中删除UISceneSession生命周期代码

    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.
         let mainStoryBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
         let loginController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController")
         self.window?.rootViewController = loginController
         self.window?.makeKeyAndVisible()
         return true
    }
    }
    

确保将 "HomeViewController" storyboardID 提供给您的视图控制器。

这就是您的 AppDelegate.swift 文件现在的样子。你已准备好出发!

如果您不想使用 sceneDelegate,那么您可以删除所有 sceneDelegate 并从 info.plist

中删除 'Application Scene Manifest'

设置UIWindow变量

var window: UIWindow?

确保您已从 info.plist 中删除 Application Scene Manifest 并更改视图控制器的背景颜色。

如果你想从应用程序中删除黑暗模式,你的设备的 Dark Mode 已启用将此密钥添加到你的 info.plist

User Interface Style = Light

你为什么不使用 SceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            guard let windowScene = (scene as? UIWindowScene) else { return }
            self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
           //Make sure to do this else you won't get 
           //the windowScene object using UIApplication.shared.connectedScenes
            self.window?.windowScene = windowScene 
            let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
            window?.rootViewController = storyBoard.instantiateInitialViewController()
            window?.makeKeyAndVisible()
        }