未调用 UITextField 选择器函数 iOS Swift

UITextField Selector Function Not Being Called iOS Swift

我创建了一个配置简单的 textField,但是我的动作函数不会被调用,不知道为什么(即我的断点甚至不会被击中)

TextField 代码:

lazy var inputField: UITextField = {
   let textField = UITextField()
    textField.borderStyle = .none
    textField.font = UIFont.systemFont(ofSize: 16)
    
    let paddingView = UIView()
    paddingView.setDimensions(height: 50, width: 28)
    textField.leftView = paddingView
    textField.leftViewMode = .always
    
    textField.addTarget(self, action: #selector(handleUpdateUseerInfo), for: .editingDidEnd)
    
    return textField
}()

动作函数代码:

@objc
func handleUpdateUseerInfo(sender: UITextField) {
    guard let valeu = sender.text else { return }
    delegate?.settingsCell(self, wantsToUpdateUserWith: valeu, for: viewModel.section)
}

选择器可能格式错误。尝试替换此行:

textField.addTarget(self, action: #selector(handleUpdateUseerInfo), for: .editingDidEnd)

有了这个:

textField.addTarget(self, action: #selector(handleUpdateUseerInfo(sender:)), for: .editingDidEnd)

这是对新 Xcode 和 swift 的更新。每当您有 addSubview() 时,将其替换为 contentView.addSubview()