gpt4 book ai didi

swift - SwiftUI用按钮更改 View

转载 作者:行者123 更新时间:2023-12-03 09:22:29 27 4
gpt4 key购买 nike

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

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




}

最佳答案

这是一种方法。

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("type here", text: $userName)
}.padding()

HStack {
Text(" Password")
TextField("type here", text: $password)
.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.")
}
}
}

关于swift - SwiftUI用按钮更改 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56797333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com