登录成功后显示新视图



我想在用户使用firebase成功登录时显示一个新视图,但是当用户成功登录时应用程序不显示另一个视图。

我尝试更改singedIn变量以检查用户是否登录,然后显示新视图,但不工作

感谢我的代码:

//
//  LoginView.swift
//  iGrow Goals
//
//  Created by George Sepetadelis on 3/8/21.
//
import SwiftUI
import Firebase
import FirebaseAuth
struct LoginView: View {

@State var email:String = ""
@State var password:String = ""
@EnvironmentObject var viewModel: AppViewModel
@State var singedIn = false


var body: some View {

NavigationView {
VStack(alignment: .center) {
Image("logo_transparent").resizable().scaledToFit()
.frame(width: 200, height: 200, alignment: .top)
.padding(.top, 150)

Text("Sign in")
.font(.system(size: 45))
.fontWeight(.bold)
.multilineTextAlignment(.center)

TextField("Email", text: $email)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)


SecureField("Password", text: $password)
.frame(height: 45)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.cornerRadius(16)
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.gray))
.padding([.horizontal], 24)
.padding(.top)
.padding(.bottom, 30)


Text("Sign in")
.font(.system(size: 25))
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 50)
.background(Color.blue)
.cornerRadius(40)
.onTapGesture {
if (email == "" || password == "") {
print("empty input")
}else {
viewModel.signIn(email: email, password: password)

if (Auth.auth().currentUser?.email != nil) {
singedIn = true
}else {
singedIn = false
print("not singed in")
}

}
}
.padding(.bottom, 500)
.ignoresSafeArea()

if singedIn {
SignUpView()
}

/*
NavigationLink(destination: LoginView().navigationBarBackButtonHidden(true)) {
Text("Go to second screen")
}*/
}
.navigationTitle("")
.navigationBarHidden(true)
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
}

SwiftUI的概念是基于改变状态变量。onTapGesture不是返回的地方;一个视图。试试这样改变isSignedIn:

的状态
@State var isSignedIn = false

Text("Sign in")
.font(.system(size: 25))
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 50)
.background(Color.blue)
.cornerRadius(40)
.onTapGesture {
if (email == "" || password == "") {
print("empty input")
}else {
viewModel.signIn(email: email, password: password)
if (Auth.auth().currentUser?.email != nil) {
isSignedIn = true
} else {
print("not singed in")
isSignedIn = false
}
}
}

//  if isSignedIn {
//      SignUpView()
//  }
// EDIT
NavigationLink("", destination: SignUpView(), isActive: $isSignedIn)

最新更新