【问题标题】:how toshow alert in SwiftUI that Email and Password are incorrect?如何在 SwiftUI 中显示电子邮件和密码不正确的警报?
【发布时间】:2026-02-02 15:55:01
【问题描述】:

晚上好,

我有一个问题,当用户输入错误的电子邮件或输入错误的密码时,我需要显示错误。我需要在我的代码中添加什么? 我是软件开发新手,Sam 对 swift ui 不太熟悉,有人可以帮我吗?

    @State var email = ""
    @State var password = ""



    var body: some View {
        Image("ISD-Logo")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 185, height: 140)
            .clipped()
            .padding(.bottom, 75)
        
        VStack {
            TextField("Email", text: $email)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            SecureField("password", text: $password)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            Button(action: { login() }) {
                Text("Sign in")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(width: 220, height: 60)
                    .background(Color.black)
                    .cornerRadius(35.0)
            }
        }
        .padding()
    }
    

    //    Login and error message
    func login() {
        Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
            if error != nil {
                print(error?.localizedDescription ?? "")
            } else {
                print("success")
                }
            }
        }
    }

}
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

【问题讨论】:

  • 你试过什么?你做了什么研究?您的代码的哪一部分不起作用?通过Minimal Reproducible Example,我们可以帮助您解决问题,但 SO 不是代码编写服务。

标签: ios swift swiftui game-development


【解决方案1】:

你想要什么样的警报?

你可以这样做:

struct ContentView: View {
    @State private var showingAlert = false

    var body: some View {
        Button("Show Alert") {
            showingAlert = true
        }
        .alert("Important message", isPresented: $showingAlert) {
            Button("OK", role: .cancel) { }
        }
    }
}

【讨论】:

  • login() 函数,如果它立即返回,则可以返回一个值,该值随后用于将显示警报设置为 true,或者显示登录后接下来出现的任何内容。但请注意,如果登录发生在互联网上,它应该异步发生,这样您就不会在登录发生时锁定主线程。如何处理这个问题远远超出了这个问题的范围。
【解决方案2】:

你可以这样试试。我建议您先阅读 SWIFT UI 基础知识

@State var email = ""
    @State var password = ""



    var body: some View {
        Image("ISD-Logo")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 185, height: 140)
            .clipped()
            .padding(.bottom, 75)
        
        VStack {
            TextField("Email", text: $email)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            SecureField("password", text: $password)
                .padding()
                .background(Color(UIColor.lightGray))
                .cornerRadius(5.0)
                .padding(.bottom, 20)
            Button(action: { login() }) {
                Text("Sign in")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(width: 220, height: 60)
                    .background(Color.black)
                    .cornerRadius(35.0)

            }
        }
        .padding()
    }
    

    //    Login and error message
    func login() {
        Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
            if error != nil {
             self.isAlert = true
                print(error?.localizedDescription ?? "")
            } else {
                print("success")
                }
            }
        }
    }

}
struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}

【讨论】:

    最近更新 更多