我如何通过点击SwiftUI中的按钮进入视图



如果用户点击connexionBtnView,我想将他们重定向到AdminViewUserView


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

}

NavigationLinkisActive参数。您可以在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()
}
}
}

相关内容

  • 没有找到相关文章

最新更新