我有用于登录的 TextFields 和用于密码的 SecureField。 当我的登录名和密码正确时,如何转到另一个 View ?
struct LoginBoard: View {
@State private var login = "Tony"
@State private var password = "1234"
var body: some View {
ZStack {
VStack {
HStack {
Text("Enter Login and Password")
}
HStack {
Image(systemName: "person")
TextField("Login", text: $login)
}
HStack {
SecureField("Password", text: $password)
}
Button("Login") {
}
}
}
}
}
最佳答案
您应该使用 NavigationView
,它相当于 UIKit 中的导航 Controller ,并使用 NavigationLink 作为导航的 segue 或触发器。
struct LoginBoard: View {
@State private var login = "Tony"
@State private var password = "1234"
@State isLoginSuccess = false
var body: some View {
// like navigation controller
// that handles the navigation of views
NavigationView {
// DestinationView is the view will go
// to if credentials is correct
NavigationLink(destination: DestinationView(),
isActive: $isLoginSuccess) { }
ZStack {
VStack {
HStack {
Text("Enter Login and Password")
}
HStack {
Image(systemName: "person")
TextField("Login", text: $login)
}
HStack {
SecureField("Password", text: $password)
}
Button("Login") {
// if user and password are correct change
// isLoginSuccess to true and will navigate
// to the next View
isLoginSuccess = true
}
}
}
}
}
}
关于swift - 登录屏幕 SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72872898/