如果用户点击connexionBtnView
,我想将他们重定向到AdminView
或UserView
import SwiftUI
struct ConnexionView: View {
@State var loginId: String = ""
@State var pwd: String = ""
@StateObject private var keyboardHander = KeyBoardHandler()
var body: some View {
NavigationView{
ZStack{
Image("background")
.ignoresSafeArea()
VStack (spacing: 15){
Spacer()
logoView
Spacer()
titleView
loginIdView
loginPwdView
connexionBtnView
Spacer()
NavigationLink {
LostPwdView()
} label: {
lostPwd
}
Spacer()
}.frame(maxHeight: .infinity)
.padding(.bottom,keyboardHander.keyboardHeight)
.animation(.default)
}
}
}
NavigationLink
有isActive
参数。您可以在NavigationLink
的初始化中传递它,当这个状态变量具有true
值时,您将重定向到另一个视图。细节。
struct ConnexionView: View {
@State var isActive: Bool = false
var body: some View {
NavigationView {
VStack {
NavigationLink(isActive: $isActive) {
LostPwdView()
} label: {
Text("Some Label")
}
Button("Tap me!") {
isActive = true
}
}
}
}
}
struct LostPwdView: View {
var body: some View {
Text("Hello")
}
}
你需要做的是有一个@State
变量来触发导航:
.fullScreenCover(
isPresented: $viewShown
) {
print("View dismissed")
} content: {
NextView()
}
其中NextView()
是你想显示的视图,viewShown
是你的状态变量,下面是一个完整的例子:
struct ExampleView: View {
@State var isNextPageOpen = false
var body: some View {
Button("Tap Here To Navigate") {
isNextPageOpen = true
}
.fullScreenCover(
isPresented: $isNextPageOpen
) {
print("View dismissed")
} content: {
NextView()
}
}
}