首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取具有Firebase身份验证的登录表单的swiftUI TextField和SecureField中的值

读取具有Firebase身份验证的登录表单的swiftUI TextField和SecureField中的值
EN

Stack Overflow用户
提问于 2021-03-10 19:46:21
回答 1查看 179关注 0票数 0

我在swiftUI中实现了一个自定义的Viwe TextField / CustomField:

代码语言:javascript
复制
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注册的视图,代码是:

代码语言:javascript
复制
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
        }
    }
}

}

但是我无法在函数寄存器()中传递输入的值email和password,在调用"Auth.auth().createUser()“时,我如何获取这种情况下变量的”作用域“?

帮帮我!请。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-10 21:17:41

所以在评论中获得所需的信息之后,是的,我确信你的问题出在你的ExtraTextField

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

然后构建并让Xcode生成一些错误,告诉您缺少参数。点击自动修复,让Xcode添加丢失的参数。

然后分别添加$emailpassw

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

让我知道这是否有效,我没有在这里复制和粘贴代码,以避免Firebase的错误,但请让我知道

使用代码更新:

代码语言:javascript
复制
     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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66564132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档