[英]TextField Value Going to be empty, SwiftUI
I am using ObservableObject to stored value of textField Here is my code in ObservableObject我正在使用 ObservableObject 来存储 textField 的值这是我在 ObservableObject 中的代码
class SignupViewModel: ObservableObject {
@Published var fullname = ""
@Published var birthday = ""
@Published var email = ""
@Published var password = ""
@Published var birthDate = Date()
@Published var isValidEmail = true
@Published var isEmailAlreadyExist = false
@Published var isPassword1 = true
@Published var isPassword2 = true
@Published var isDisable: Bool = false
}
I had created instance like this and used them我已经创建了这样的实例并使用了它们
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 5) {
TextFieldWithoutValidation(text: $signupModel.fullname, placeHolder: "Full name", keyBoardType: .default)
.accentColor(Color.black)
// LabelTextField(title: "Full name", text: $signupModel.fullname, keyboardType: .default, contentType: nil)
// TextField("Full name", text: $data.value)
Text("We’ll use this to issue you a cheque when you make a profit.")
.font(.system(size: 11.0))
}
DateView(titleText: "Birthday", date: $signupModel.birthDate, labelText: $signupModel.birthday)
TextFieldWithoutValidation(text: $signupModel.email, placeHolder: "Email", keyBoardType: .emailAddress)
.accentColor(Color.black)
if !signupModel.isValidEmail {
errorView(.emailNotValid)
}
SecureTextFieldWithoutValidation(text: $signupModel.password, placeHolder: "Password")
.accentColor(Color.black)
if !signupModel.password.isEmpty {
VStack(spacing: 0) {
errorView((signupModel.isPassword1 ? .passwordRight1 : .passwordWrong1))
errorView((signupModel.isPassword2 ? .passwordRight2 : .passwordWrong2))
}
}
createButton()
}
.padding(.leading, 20) // you can use as per your requirement(-16)
.padding(.trailing, 20)
.titleForNavigation("Create your account", displayMode: .large)
.navigationLayout(AnyView(backButton))
}
But when i am typing something in textField and go to the next textField previous textField text going to be empty automatically但是当我在 textField 和 go 中输入内容到下一个 textField 之前的 textField 文本将自动为空
can someone help why it's happening like this有人可以帮助解释为什么会这样吗
I am not sure if this answer helps, I was able to replicate your example within a playground.我不确定这个答案是否有帮助,我能够在操场上复制您的示例。
The object appears to retain its state between renders and after pressing the 'Save' button. object 似乎在渲染之间和按下“保存”按钮后保留其 state。
import PlaygroundSupport
class SignupViewModel: ObservableObject {
@Published var fullname = ""
@Published var birthday = ""
@Published var email = ""
@Published var password = ""
@Published var birthDate = Date()
@Published var isValidEmail = true
@Published var isEmailAlreadyExist = false
@Published var isPassword1 = true
@Published var isPassword2 = true
@Published var isDisable: Bool = false
}
struct ContentView: View {
@ObservedObject var signupModel = SignupViewModel()
var body: some View {
VStack{
Text("Sign up form")
TextField("Name", text: $signupModel.fullname)
TextField("Email", text: $signupModel.email)
TextField("Password", text: $signupModel.password)
Button(action: { print("Form data:"); print("\(signupModel.fullname) \(signupModel.email) \(signupModel.password)") }) {
Text("Save")
}
}
}
}
let viewController = UIHostingController(rootView: ContentView())
PlaygroundPage.current.liveView = viewController
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.