简体   繁体   English

登录后如何在 SwiftUI 中呈现新视图

[英]How to present new view in SwiftUI after login

I need to know who to present a new view using SwiftUI (following MVVM design pattern).我需要知道谁使用 SwiftUI(遵循 MVVM 设计模式)呈现新视图。

For example, once the user enters the credentials to login, pressing the "login" button should take them to the home page of the app without being able to return to the login page.例如,一旦用户输入凭据进行登录,按下“登录”按钮应该会将他们带到应用程序的主页,而无法返回登录页面。

I have been looking at the documentation from Apple, as well as other questions in Stackoverflow, but they all seem to be using navigation views (ie there is a navigation bar at the top which allows the user to return to the previous view).我一直在查看来自 Apple 的文档以及 Stackoverflow 中的其他问题,但它们似乎都在使用导航视图(即顶部有一个导航栏,允许用户返回上一个视图)。

Below is the code of how I would have accomplished this using UIKit, as well as an image to visually represent what I want to accomplish.下面是我将如何使用 UIKit 完成此操作的代码,以及一个直观地表示我想要完成的图像。

let vc = MainMenuController()

vc.modalPresentationStyle =.fullScreen

self.present(vc, animated: true, completion: nil);

Sample image of desired outcome期望结果的示例图像

this is how i implemented it in my code in ContentView.swift :这就是我在ContentView.swift的代码中实现它的方式:

@State var loggedIn: Bool = false

var body: some View {
  VStack {
    if loggedIn {
      ShowSomeTabView()
    } else {
      ShowLoginScreen()
    }
  }.onAppear {
    self.checkAuthorized()
  }
}

private func checkAuthorized() {
  if loggedIn() {
    self.loggedIn = true
  }
}

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

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