so I have a TextField
and a SecureField
, and both will randomly cut off the last character:
TextField("email address", text: $userInfo.email)
.disableAutocorrection(true)
.autocapitalization(.none)
.keyboardType(.emailAddress)
.foregroundColor(Color.buttonText)
}.textFieldStyle(RoundedBorderTextFieldStyle()).padding().padding(.top, 50)
and
SecureField("password", text: $userInfo.password)
.disableAutocorrection(true)
.autocapitalization(.none)
.foregroundColor(Color.buttonText)
}.textFieldStyle(RoundedBorderTextFieldStyle()).padding()
for example, if I entered my email as [email protected]
and my password as password123
, my print statements in other parts of the code would say [email protected]
and password12
.
It also says
Binding action tried to update multiple times per frame.
any time I type a key, this might be related. I have only tested on simulators.
edit: user class
class User: ObservableObject{
var id = UUID()
var email : String
var password : String
@Published var loggedIn : Bool
@Published var image: UIImage = UIImage(named: "user")!
init(email: String = "", loggedIn : Bool = false, password : String = ""){
self.email = email
self.loggedIn = loggedIn
self.password = password
}
it is an environment object, if that affects it at all.
CodePudding user response:
The user email and password needed @published
CodePudding user response:
Ques:- Binding action tried to update multiple times per frame.
Ans:-
If I decorate my username and password properties with @Published then the message goes away
This is the correct solution. You need to use @Published
on those properties because that is how SwiftUI gets notified when the properties change.
the body is rendered each time I type in the TextField
That is fine. Your body
method is not expensive to compute.
I don't think I will gain anything from putting the @Published attribute since this is a one-way binding
You cannot be sure SwiftUI will work correctly (now or in future releases) if you don't use @Published
. SwiftUI expects to be notified when the value of a Binding
changes, even when a built-in SwiftUI component like TextField
causes the change.
Ques:- so I have a TextField and a SecureField, and both will randomly cut off the last character
Ans:-
import SwiftUI
struct SecureTextfliedWithEmail: View {
@StateObject var userInfo = User()
var body: some View {
VStack {
TextField("email address", text: $userInfo.email)
.disableAutocorrection(true)
.autocapitalization(.none)
.keyboardType(.emailAddress)
.foregroundColor(Color.red)
.textFieldStyle(RoundedBorderTextFieldStyle()).padding().padding(.top, 50)
SecureField("password", text: $userInfo.password)
.disableAutocorrection(true)
.autocapitalization(.none)
.foregroundColor(Color.blue)
.textFieldStyle(RoundedBorderTextFieldStyle()).padding()
Button("Print") {
print(userInfo.email,userInfo.password)
}
}
}
}
class User: ObservableObject{
var id = UUID()
@Published var email : String
@Published var password : String
@Published var loggedIn : Bool
init(email: String = "", loggedIn : Bool = false, password : String = ""){
self.email = email
self.loggedIn = loggedIn
self.password = password
}
}