[英]SwiftUI - detect change in Textfield
I want when user edit information in textfield it will listen for change -> Save button will change from Gray to Blue and enable.我希望当用户在文本字段中编辑信息时,它会监听更改 -> 保存按钮将从灰色变为蓝色并启用。 If the user has not edited the information, the save button will disable.如果用户尚未编辑信息,则保存按钮将禁用。
I want when user edit information in textfield it will listen for change -> Save button will change from Gray to Blue.我希望当用户在文本字段中编辑信息时,它会监听更改 -> 保存按钮将从灰色变为蓝色。 If the user has not edited the information, the save button will disable如果用户没有编辑信息,保存按钮将禁用
here is a simple example that maybe of use to you, using some of the comments advice.这是一个简单的例子,可能对你有用,使用一些评论建议。 There are 3 main elements, the condition, the disabling and the color.有3个主要元素,条件,禁用和颜色。
struct ContentView: View {
@State private var password = ""
@State private var passwordRepeat = ""
// here the condition you want
var isConfirmed: Bool {
if !password.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty &&
!passwordRepeat.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty &&
(password == passwordRepeat) {
return true
} else {
return false
}
}
var body: some View {
VStack {
SecureField("password", text: $password)
SecureField("password confirm", text: $passwordRepeat)
Button(action: { } ) {
Text("SAVE").accentColor(.white)
}
.disabled(!isConfirmed) // <-- here to enable/disable the button
.padding(20)
.background(isConfirmed ? Color.green : Color.gray) // <-- here to change color of the button
.cornerRadius(20)
}.frame(width: 333)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
EDIT1:编辑1:
here is an approach to make the condition work with when your Textfield already has content in it:当您的文本字段中已经包含内容时,这是一种使条件起作用的方法:
// your ProfileEditorRow equivalent
struct ContentView: View {
@ObservedObject var profileViewModel: ProfileViewModel
let refUserName: String // <-- reference user name
init(profileViewModel: ProfileViewModel) {
self.profileViewModel = profileViewModel
self.refUserName = profileViewModel.user.name // <-- here keep the initial user name
}
var isCondition: Bool {
return (refUserName == profileViewModel.name) // <-- here test for change
}
var body: some View {
VStack {
TextField("test",text: $profileViewModel.user.name)
Button(action: { print("----> SAVE") } ) {
Text("SAVE").accentColor(.white)
}
.disabled(isCondition) // <-- here to enable/disable the button
.padding(20)
.background(isCondition ? Color.gray : Color.green) // <-- here to change color of the button
.cornerRadius(20)
}.frame(width: 333)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
EDIT2: sample with multiple elements in condition EDIT2:条件中具有多个元素的样本
You probably have a User
struct with name, birthday etc... using this:你可能有一个带有姓名、生日等的User
结构......使用这个:
struct User {
var name
var birthday
var gender
// ....
}
struct ContentView: View {
@ObservedObject var profileViewModel: ProfileViewModel
let refUser: User // <-- reference user
init(profileViewModel: ProfileViewModel) {
self.profileViewModel = profileViewModel
self.refUser = profileViewModel.user // <-- here keep the initial user values
}
var isCondition: Bool {
return (refUser.name == profileViewModel.user.name) &&
(refUser.birthday == profileViewModel.user.birthday)
}
var body: some View {
VStack {
TextField("name",text: $profileViewModel.user.name)
TextField("birthday",text: $profileViewModel.user.birthday)
Button(action: { print("----> SAVE") } ) {
Text("SAVE").accentColor(.white)
}
.disabled(isCondition) // <-- here to enable/disable the button
.padding(20)
.background(isCondition ? Color.gray : Color.green) // <-- here to change color of the button
.cornerRadius(20)
}.frame(width: 333)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.