swift - 登录屏幕 SwiftUI

标签 swift swiftui

我有用于登录的 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/

相关文章:

ios - 如何从以编程方式制作的按钮设置转场

swift - 通知字典中的参数 Any 还是 Bool?

ios - 如何从 SwiftUI 调用 PHPhotoLibrary presentLimitedLibraryPicker?

SwiftUI 预览失败并出现 __designTimeString 错误

ios - 如何在 SwiftUI 中禁用带有切换的文本字段?

swift - 选择地点错误 - Google Places API

xcode - 实例成员不能用于类型

ios - 如何打开 .swiftmodule 文件

ios - 过滤的关系计数未在父 View 中更新

swiftui - 带有 SegmentedPickerStyle() 的选取器不会在 SwiftUI 中显示选取器标题