SwiftUI Change View with Button

前端 未结 4 1862
失恋的感觉
失恋的感觉 2021-02-04 18:48

I understand there is PresentationButton and NavigationButton in order to change views in the latest SwiftUI. However I want to do a simple operation like below. When user click

4条回答
  •  清歌不尽
    2021-02-04 19:13

    You can use navigation link with tags so,

    Here is the code:

    first of all, declare tag var

    @State var tag : Int? = nil
    

    then create your button view:

    Button("Log In", action: {
                            Auth.auth().signIn(withEmail: self.email, password: self.password, completion: { (user, error) in
                                if error == nil {
                                    self.tag = 1
                                    print("success")
                                }else{
                                    print(error!.localizedDescription)
                                }
                            })
    

    So when log in success tag will become 1 and when tag will become 1 your navigation link will get executed

    Navigation Link code:

    NavigationLink(destination: HomeView(), tag: 1, selection: $tag) {
                    EmptyView()
                    }.disabled(true)
    

    if you are using Form use .disabled because here the empty view will be visible on form and you don't want your user to click on it and go to the homeView.

提交回复
热议问题