使用 Firebase Auth 读取 swiftUI TextField 和 SecureField 中的值以获取登录表单

read value in swiftUI TextField and SecureField for login form with Firebase Auth

我已经在 swiftUI 中实现了自定义 Viwe TextField / CustomField:

import SwiftUI

struct ExtraTextField: View {
    
    @State public var mytext: String
    
    let textTitle: String
    let icon: String
    let secure: Bool
    
    var body: some View {
        VStack(alignment: .leading) {
                HStack {
                    Image(systemName: icon)
                    if (secure){
                        SecureField(textTitle, text: $mytext)
                    }else{
                        TextField(textTitle, text: $mytext)
                    }
                }.modifier(customViewModifier(roundedCornes: 6, startColor: .yellow, endColor: .orange, textColor: Color(red: 0.17, green: 0.24, blue: 0.31)))
            }.padding()
        }
}

struct customViewModifier: ViewModifier {
    var roundedCornes: CGFloat
    var startColor: Color
    var endColor: Color
    var textColor: Color
    
    func body(content: Content) -> some View {
        content
            .padding()
            .background(LinearGradient(gradient: Gradient(colors: [startColor, endColor]), startPoint: .topLeading, endPoint: .bottomTrailing))
            .cornerRadius(roundedCornes)
            .padding(3)
            .foregroundColor(textColor)
            .overlay(RoundedRectangle(cornerRadius: roundedCornes)
                        .stroke(LinearGradient(gradient: Gradient(colors: [startColor, endColor]), startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 2.5))
            .font(.custom("Open Sans", size: 15))
            .disableAutocorrection(true)
            .autocapitalization(.none)
            //.shadow(radius: 10)
    }
}

在另一个文件 swiftUI 中,我实现了一个用于在 Fidebase 注册的视图,代码是:

struct RegisterView: View {

@State private var isShowMainView = false

@State var email: String
@State var passw: String

var body: some View {
    VStack{
        NavigationLink(destination: MainView(),isActive: $isShowMainView){}
        ExtraTextField(mytext: email, textTitle: "username", icon: "person.crop.circle.fill", secure: false)
        ExtraTextField(mytext: passw, textTitle: "password", icon: "key.fill", secure: true)
        
        Button(action: {
                print("email  :\(email)")
                print("password:\(passw)")
                register()}){
            ExtraViewButton(namebutton: "Registrati", iconbutton: "person.crop.circle.fill")
        }
    }
    .padding()
}

func register(){
    Auth.auth().createUser(withEmail: email, password: passw) { authResult, error in
        if error != nil {
            print(error?.localizedDescription ?? "")
            print("email  :\(email)")
            print("password:\(passw)")
        } else{
            // registrazione riuscita accesso all'app
            self.isShowMainView = true
        }
    }
}

}

但我无法在调用“Auth.auth().createUser()”时在 func register() 中传递输入的值电子邮件和密码,如何获取变量的“范围”在这种情况下?

帮帮我!请

因此,在获得评论中所需的信息后,是的,我确定您的问题出在 ExtraTextField

@State public var mytext: String 更改为 @Binding var my text: String

然后构建,让Xcode产生一些错误让你说你缺少参数。点击自动修复并让 Xcode 添加缺少的参数。

然后分别加入$emailpassw

如果你使用预览,你会得到另一个错误.. 只需传入 `constant("") 即可通过。

如果可行请告诉我,我没有在此处复制和粘贴代码以避免 Firebase 出现错误,但请告诉我

更新代码:

     struct ExtraTextField: View {
        
        @Binding public var mytext: String
        
        let textTitle: String
        let icon: String
        let secure: Bool
        
        var body: some View {
            VStack(alignment: .leading) {
                    HStack {
                        Image(systemName: icon)
                        if (secure){
                            SecureField(textTitle, text: $mytext)
                        }else{
                            TextField(textTitle, text: $mytext)
                        }
                    }.modifier(customViewModifier(roundedCornes: 6, startColor: .yellow, endColor: .orange, textColor: Color(red: 0.17, green: 0.24, blue: 0.31)))
                }.padding()
            }
    }

struct ETPreviews: PreviewProvider {
    static var previews: some View {
        ExtraTextField(mytext: .constant(""), textTitle: "", icon: "", secure: false)
    }
}