Ati*_*ils 1 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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这是一种方法。
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.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的一个应用程序中有同样的需求,我找到了一个解决方案......
基本上你需要在 NavigationView 中插入你的主视图,然后在你的视图中添加一个不可见的 NavigationLink,创建一个 @state var 来控制你何时想要推送视图并在你的登录回调中更改它的值......
那是代码:
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)
}
}
Run Code Online (Sandbox Code Playgroud)