如何在没有导航回溯的情况下启动 SwiftUI 视图?

How can I launch a SwiftUI View without Navigation back tracking?

我想将视图作为没有导航层次结构的独立视图启动。我不想使用 NavigationButton 的原因是我不希望用户 return 到调用表单。

我尝试了以下类似于在 ScenceDelegate 中启动第一个视图的方法,但没有任何反应:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let window = appDelegate.getNewWindow()
    window.rootViewController = UIHostingController(rootView: NewView())
    window.makeKeyAndVisible()

我有不使用导航的正当理由 UI,为了简短起见,我将省略解释。我避免使用 Storyboard 以尽可能简单。

感谢您提出任何解决方案建议。

这就是我加载新根视图的方式。

我在 AppDelegate 代码中添加了以下内容

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
     var window: UIWindow?

   ...

    func loadNewRootSwiftUIView(rootViewController: UIViewController)
    {
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = rootViewController
        self.window = window
        window.makeKeyAndVisible()
    }
}

并将以下内容放入我的表单中:

struct LaunchView : View {
    var body: some View {
        VStack {
            Button(
                action: {
                   LaunchLoginView()
            },
                label: {
                    Text("Login")
            }
            )
        }
    }
}

func LaunchLoginView(){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let vContoller = UIHostingController(rootView: LoginView())
    appDelegate.loadNewRootSwiftUIView(rootViewController: vContoller)
}