问题描述
我有一个登录和注册视图。如果用户成功登录,则需要通过NavigationLink将其带到homeMainView,如果需要注册,则将在表单中显示注册视图。登录和注册时的身份验证都可以正常工作,但是当显示注册表并且用户成功登录时,我无法从显示的表中将Navlink的视图获取到homeMainView。该视图可以从登录视图中正常浏览,但不能从注册中导航。我尝试了几种不同的方法,但没有成功。这是我的代码
struct LoginView: View {
@State private var email = ""
@State private var password = ""
@State private var formOffset: CGFloat = 0
@State private var presentSignupSheet = false
@State private var presentPasswordRecoverySheet = false
@State var isNavigationBarHidden: Bool = true
@State var isLinkActive = false // <- add here
var body: some View {
NavigationView { // <- wrap in the `NavigationView`
vstack(spacing: 40) {
vstack {
LCTextfield(value: self.$email,placeholder: "Email",icon: Image(systemName: "at"),onEditingChanged: { flag in
withAnimation {
self.formOffset = flag ? -150 : 0
}
})
LCTextfield(value: self.$password,placeholder: "Password",icon: Image(systemName: "lock"),isSecure: true)
LCButton(text: "Login") {
self.handleLogin(username: self.email,password: self.password)
}
}
Button(action: {
self.presentSignupSheet.toggle()
}) {
HStack {
Text("Don't have an account? Sign up.").accentColor(Color.PrimaryColor)
}
}.sheet(isPresented: self.$presentSignupSheet) {
SignupView()
}
Button(action: {
self.presentPasswordRecoverySheet.toggle()
}) {
HStack {
Text("Forgot your password?").accentColor(Color.PrimaryColor)
}
}.sheet(isPresented: self.$presentPasswordRecoverySheet) {
RecoverPasswordView(presentPasswordRecoverySheet: self.$presentPasswordRecoverySheet)
}
}.padding().offset(y: self.formOffset)
.background(Color.sNowColor).edgesIgnoringSafeArea(.all)
.background( // add a hidden `NavigationLink` in the background
NavigationLink(destination: homeMainView(),isActive: $isLinkActive) {
EmptyView()
}
.hidden()
)
}.navigationBarTitle("",displayMode: .inline) // optionally set title
.navigationBarHidden(self.isNavigationBarHidden)
.onAppear {
self.isNavigationBarHidden = true
}
}
struct SignupView: View {
@State private var fname = ""
@State private var lname = ""
@State private var email = ""
@State private var password = ""
@State private var confirmedPassword = ""
@Environment(\.presentationMode) var presentationMode
@State private var formOffset: CGFloat = 0
@State var isLinkActive = false // <- add here
var body: some View {
vstack(spacing: 40) {
vstack {
HStack {
LCTextfield(value: self.$fname,placeholder: "First Name")
LCTextfield(value: self.$lname,placeholder: "Last Name")
}
LCTextfield(value: self.$email,onEditingChanged: { flag in
withAnimation {
self.formOffset = flag ? -150 : 0
}
})
LCTextfield(value: self.$password,isSecure: true)
LCButton(text: "Sign up") {
self.handleSignUp(username: self.email,password: self.password)
}
}
Button(action: {
}) {
HStack {
Text("Already have an account?").accentColor(Color.accentColor)
}
}
}.padding().offset(y: self.formOffset)
.navigationBarTitle("",displayMode: .inline) // optionally set title
.navigationBarHidden(true)
.background( // add a hidden `NavigationLink` in the background
NavigationLink(destination: homeMainView(),isActive: $isLinkActive) {
EmptyView()
}
.hidden()
)
.background(Color.sNowColor).edgesIgnoringSafeArea(.all)
}
func handleSignUp(username: String,password: String) {
app.usernamePasswordProviderClient().registerEmail(email,password: password,completion: { (error) in
// Completion handlers are not necessarily called on the UI thread.
// This call to dispatchQueue.main.sync ensures that any changes to the UI,print("Signup successful!")
// Registering just registers. Now we need to sign in,but we can reuse the existing username and password.
self.presentationMode.wrappedValue.dismiss()
LoginView().handleLogin(username: self.email,password: self.password)
self.isLinkActive = true // <- activate the `NavigationLink`
}
})
}
成功注册用户后,将用户导航到homeMainView的最佳方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)