【问题标题】:Setting rootviewcontoller in AppDelagate after deleting SceneDelegate删除 SceneDelegate 后在 AppDelagate 中设置 rootviewcontoller
【发布时间】:2024-01-21 15:34:01
【问题描述】:

我删除了 SceneDelegate.swift 并从 .plist 文件中删除了相关条目。

由于我的项目不支持 iPad,我不需要任何 SceneDelegate 东西。我说的对吗?

我使用 SwiftUI,所以也没有 Storyboard

现在在 AppDelegate 文件中

var window: UIWindow?

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

        self.window = UIWindow(frame: UIScreen.main.bounds)

  if (TegKeychain.get("ISLOGGEDIN") == "1") {

           //present WelcomeScreen.swift

        } else {

            //present Login.Swift
        }

        return true
    }

其他文件

struct Login: View {
    var body: some View {
        Text("Login")
    }
}

struct WelcomeScreen: View {
    var body: some View {
        Text("WelcomeScreen")
    }
}

我搜索了网络,但找不到任何有关如何在没有情节提要的情况下呈现视图控制器的有用信息。任何帮助将不胜感激。

【问题讨论】:

  • //present WelcomeScreen.swift 里面有什么

标签: ios swift swiftui appdelegate


【解决方案1】:

这是我为从模板创建的新 SwiftUI iOS 项目回滚场景支持所做的检查清单:

  1. 从 Info.plist 中移除 UIApplicationSceneManifest 键

  2. 从 AppDelegate 中移除所有与场景会话相关的委托方法

  3. 删除 SceneDelegate

  4. 更新 AppDelegate 如下

    import UIKit
    import SwiftUI

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            let contentView = ContentView() // << default Hello World

            // Use a UIHostingController as window root view controller.
            let window = UIWindow(frame: UIScreen.main.bounds)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()

            return true
        }

    }
  1. 构建并运行 - 工作。

【讨论】: