Skip to content

1.2_手动配置SceneDelegate与AppDelegate

在纯代码的 UIKit 项目中,SceneDelegateAppDelegate 扮演着至关重要的角色。它们负责管理应用程序的生命周期,并处理各种系统事件。让我们一起深入了解如何手动配置它们,确保你的应用能够顺利启动和运行!🚀

理解 AppDelegate 的职责

AppDelegate 是你的应用程序的“总指挥”。它负责处理应用程序级别的事件,例如:

  • 应用程序启动 (application:didFinishLaunchingWithOptions:):这是应用程序启动的第一个入口点。你可以在这里进行一些全局性的初始化设置。
  • 应用程序进入后台 (applicationWillResignActive:) 和前台 (applicationDidBecomeActive:):处理应用程序状态的切换。
  • 内存警告 (applicationDidReceiveMemoryWarning:):当系统内存不足时,你会在这里收到通知。

配置 AppDelegate

  1. 创建 AppDelegate 文件:如果你的项目中没有 AppDelegate.swift 文件,你需要手动创建一个。

  2. 修改 AppDelegate 类:确保你的 AppDelegate 类遵循 UIApplicationDelegate 协议。

    swift
    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // 在这里进行初始化设置
            return true
        }
    
        // 其他 AppDelegate 方法...
    }

SceneDelegate 的作用

SceneDelegate 负责管理应用程序的场景(Scene)。一个场景代表应用程序的一个用户界面实例。在 iOS 13 及更高版本中,SceneDelegate 取代了 AppDelegate 的部分职责,特别是与 UI 生命周期相关的部分。

配置 SceneDelegate

  1. 创建 SceneDelegate 文件:如果你的项目中没有 SceneDelegate.swift 文件,你需要手动创建一个。

  2. 修改 SceneDelegate 类:确保你的 SceneDelegate 类遵循 UIWindowSceneDelegate 协议。

    swift
    import UIKit
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are (re)connecting from a previous state, as they may be new.
            guard let windowScene = (scene as? UIWindowScene) else { return }
    
            window = UIWindow(frame: windowScene.coordinateSpace.bounds)
            window?.windowScene = windowScene
            window?.rootViewController = ViewController() // 设置根视图控制器
            window?.makeKeyAndVisible()
        }
    
        // 其他 SceneDelegate 方法...
    }

连接 AppDelegate 和 SceneDelegate

确保你的 AppDelegate 能够正确地创建和管理 SceneDelegate。在 AppDelegateapplication:configurationForConnectingSceneSession:options: 方法中,你需要返回一个 UISceneConfiguration 对象,告诉系统使用哪个 SceneDelegate 类来管理场景。

swift
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)
}

移除 Storyboard 引用

为了确保你的项目完全使用代码配置 UI,你需要移除项目设置中对 Storyboard 的引用。

  1. 打开你的项目设置。
  2. 在 "General" 选项卡中,找到 "Deployment Info" 部分。
  3. 清空 "Main Interface" 字段。

验证配置

编译并运行你的应用程序。如果一切配置正确,你应该能够看到你的根视图控制器显示在屏幕上。🎉

通过以上步骤,你已经成功地手动配置了 SceneDelegateAppDelegate,为你的纯代码 UIKit 项目奠定了坚实的基础。记住,理解这些组件的作用对于构建健壮和可维护的 iOS 应用程序至关重要。

本站使用 VitePress 制作