I am using ObservableObject to stored value of textField Here is my code in 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
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.