SwiftUI用按钮更改视图

时间:2019-06-27 19:09:16

标签: swift swiftui xcode11

我了解到有PresentationButton和NavigationButton可以在最新的SwiftUI中更改视图。但是我想做一个简单的操作,如下所示。如果凭据正确,则用户单击“登录”按钮时,它将登录它们,但还会进行登录(在这种情况下,请更改视图)。但是,我无法在PresentationButton中检查它们是否正确,并且无法在普通按钮中更改视图。还有另一种方法吗?

  @IBAction func signInClicked(_ sender: Any) {

        if emailText.text != "" && passwordText.text != "" {

            Auth.auth().signIn(withEmail: emailText.text!, password: passwordText.text!) { (userdata, error) in
                if error != nil {
                   //error
                } else {

                   performSegue(withIdentifier: "toFeedActivity", sender: nil)


                }
            }

        } else {
            //error
        }




    }

4 个答案:

答案 0 :(得分:1)

这是一种方式。

struct AppContentView: View {

    @State var signInSuccess = false

    var body: some View {
        return Group {
            if signInSuccess {
                AppHome()
            }
            else {
                LoginFormView(signInSuccess: $signInSuccess)
            }
        }
    }
}

struct LoginFormView : View {

    @State private var userName: String = ""
    @State private var password: String = ""

    @State private var showError = false

    @Binding var signInSuccess: Bool

    var body: some View {
        VStack {
            HStack {
                Text("User name")
                TextField($userName, placeholder: Text("type here"))
            }.padding()

            HStack {
                Text(" Password")
                TextField($password, placeholder: Text("type here"))
                    .textContentType(.password)
            }.padding()

            Button(action: {
                // Your auth logic
                if(self.userName == self.password) {
                    self.signInSuccess = true
                }
                else {
                    self.showError = true
                }

            }) {
                Text("Sign in")
            }

            if showError {
                Text("Incorrect username/password").foregroundColor(Color.red)
            }
        }
    }
}

struct AppHome: View {

    var body: some View {
        VStack {
        Text("Hello freaky world!")
        Text("You are signed in.")
        }
    }
}

答案 1 :(得分:1)

我的一个应用程序也有同样的需求,并且找到了解决方案...

基本上,您需要将主视图插入NavigationView中,然后在视图中添加一个不可见的NavigationLink,创建一个@state var来控制何时推送视图并在登录回调中更改其值... < / p>

那是代码:

struct ContentView: View {
    @State var showView = false
    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    print("*** Login in progress... ***")
                    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                        self.showView = true
                    }
                }) {
                    Text("Push me and go on")
                }

                //MARK: - NAVIGATION LINKS
                NavigationLink(destination: PushedView(), isActive: $showView) {
                    EmptyView()
                }
            }
        }
    }
}


struct PushedView: View {
    var body: some View {
        Text("This is your pushed view...")
            .font(.largeTitle)
            .fontWeight(.heavy)
    }
}

答案 2 :(得分:0)

尝试使用状态和.sheet

struct ContentView: View {
    @State var showingDetail = false

    var body: some View {
        Button(action: {
            self.showingDetail.toggle()
        }) {
            Text("Show Detail")
        }.sheet(isPresented: $showingDetail) {
            DetailView()
        }
    }
}

答案 3 :(得分:-1)

您可以在标签中使用导航链接,

代码如下:

首先,声明标签var

@State var tag : Int? = nil

然后创建您的按钮视图:

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)
                            }
                        })

因此,当成功登录标签将变为1且当标签变为1时,导航链接将被执行

导航链接代码:

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

如果您使用的是Form,请使用.disabled,因为在这里空视图将在窗体上可见,并且您不希望用户单击它并转到homeView。