How to push a new root view using SwiftUI without NavigationLink?

后端 未结 2 1758
你的背包
你的背包 2020-12-17 06:40

I have a login screen. After user fill up credential, I want to verify it then start a new root view so user won\'t be able to navigate back to the login view. I currently

2条回答
  •  温柔的废话
    2020-12-17 07:26

    Another approach with Notifications is to place the observer inside your RootView and check for changes in a variable to decide which View should be presented, here is a very simplified example:

    struct RootView: View {
    
        @State var isLoggedIn: Bool = false
    
        var body: some View {
            Group {
                VStack{
                    if isLoggedIn {
                        Text("View B")
                        Button(action: {
                            NotificationCenter.default.post(name: NSNotification.Name("changeLogin"), object: nil)
                        }) {
                            Text("Logout")
                        }
                    } else {
                        Text("View A")
                        Button(action: {
                            NotificationCenter.default.post(name: NSNotification.Name("changeLogin"), object: nil)
                        }) {
                            Text("Login")
                        }
                    }
                }
            }.onAppear {
                NotificationCenter.default.addObserver(forName: NSNotification.Name("changeLogin"), object: nil, queue: .main) { (_) in
                    self.isLoggedIn.toggle()
                }
            }
        }
    

    And load RootView in your rootViewController as usual.

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        let contentView = RootView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    

提交回复
热议问题