我在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
}
}
}}
但是我无法在函数寄存器()中传递输入的值email和password,在调用"Auth.auth().createUser()“时,我如何获取这种情况下变量的”作用域“?
帮帮我!请。
发布于 2021-03-10 21:17:41
所以在评论中获得所需的信息之后,是的,我确信你的问题出在你的ExtraTextField上
将@State public var mytext: String更改为@Binding var my text: String
然后构建并让Xcode生成一些错误,告诉您缺少参数。点击自动修复,让Xcode添加丢失的参数。
然后分别添加$email和passw。
如果你使用预览,你会得到另一个错误。只需传入` `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)
}
}https://stackoverflow.com/questions/66564132
复制相似问题