繁体   English   中英

SwiftUI - 检测文本字段中的变化

[英]SwiftUI - detect change in Textfield

我希望当用户在文本字段中编辑信息时,它会监听更改 -> 保存按钮将从灰色变为蓝色并启用。 如果用户尚未编辑信息,则保存按钮将禁用。

我希望当用户在文本字段中编辑信息时,它会监听更改 -> 保存按钮将从灰色变为蓝色。 如果用户没有编辑信息,保存按钮将禁用

这是一个简单的例子,可能对你有用,使用一些评论建议。 有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())
    }
}

编辑1:

当您的文本字段中已经包含内容时,这是一种使条件起作用的方法:

// 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:条件中具有多个元素的样本

你可能有一个带有姓名、生日等的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM