I have a UITextField
embedded within a UITableViewCell
. I am trying to implement a feature similar to Instagram's commenting where when the user has text in the textfield the send button is enabled, and if there is no text the send button is not enabled. I can't seem to detect when the user is done typing even when having implemented textFieldDidEndEditing
. This method only seems to work if the return button is clicked. Is there a way to detect when the user is done typing in the textfield and has at least one character, thereby enabling the send button?
我在UITableViewCell中嵌入了一个UITextField。我正在尝试实现类似于Instagram评论的功能,当用户在文本字段中显示文本时,启用发送按钮,如果没有文本,则不启用发送按钮。即使实现了textFieldDidEndEditing,我似乎也无法检测用户何时完成输入。如果单击返回按钮,此方法似乎只有效。有没有办法检测用户何时在文本字段中输入并且至少有一个字符,从而启用发送按钮?
My code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
case .comment:
let cell = tableView.dequeueReusableCellWithIdentifier(cellInfo.description, forIndexPath: indexPath) as! PictureInformationTableViewCell
cell.sendMessageBtn.addTarget(self, action: "didTapSendMessageBtn:", forControlEvents: .TouchUpInside)
cell
cell.commentTextField.layer.borderWidth = 1
cell.commentTextField.layer.borderColor = UIColor.whiteColor().CGColor
cell.sendMessageBtn.layer.cornerRadius = 5
if initBtn == false {
cell.sendMessageBtn.enabled = false
}
buttonColor = cell.sendMessageBtn.tintColor
cell.commentTextField.delegate = self
//cell.commentTextField.addTarget(self, action: "didEndEditing:", forControlEvents: UIControlEvents.e)
return cell
}
func textFieldDidEndEditing(textField: UITextField) {
let cell = tableView.dequeueReusableCellWithIdentifier(PictureInformation.comment.description, forIndexPath: NSIndexPath(forRow: 0, inSection: 3)) as! PictureInformationTableViewCell
if let text = textField.text {
initBtn = true
if text.characters.count > 0 {
cell.sendMessageBtn.enabled = true
cell.sendMessageBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
tableView.reloadInputViews()
} else {
cell.sendMessageBtn.enabled = false
cell.sendMessageBtn.setTitleColor(buttonColor, forState: UIControlState.Normal)
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 3)], withRowAnimation: .None)
tableView.reloadInputViews()
}
}
}
1 个解决方案
#1
0
Add the action EditingChanged
for your textField
that action is called whenever you add a value to your textField
.
为textField添加ActionChanged操作,只要向textField添加值,就会调用该操作。
Update I have added a textField and a button in my storyboard. I have created two outlets, one for the button and one for the textField and I have added an action for my textField (EditingChanged).
更新我在故事板中添加了textField和一个按钮。我创建了两个出口,一个用于按钮,一个用于textField,我为textField(EditingChanged)添加了一个动作。
The only thing you need to do now to enabled/disable the button is:
您现在唯一需要启用/禁用按钮的是:
@IBOutlet weak var btn: UIButton!
@IBOutlet weak var txtField: UITextField!
@IBAction func txtField_EditingChanged(sender: AnyObject) {
if txtField.text?.characters.count > 0{
btn.enabled = true
}
else{
btn.enabled = false
}
}
#1
0
Add the action EditingChanged
for your textField
that action is called whenever you add a value to your textField
.
为textField添加ActionChanged操作,只要向textField添加值,就会调用该操作。
Update I have added a textField and a button in my storyboard. I have created two outlets, one for the button and one for the textField and I have added an action for my textField (EditingChanged).
更新我在故事板中添加了textField和一个按钮。我创建了两个出口,一个用于按钮,一个用于textField,我为textField(EditingChanged)添加了一个动作。
The only thing you need to do now to enabled/disable the button is:
您现在唯一需要启用/禁用按钮的是:
@IBOutlet weak var btn: UIButton!
@IBOutlet weak var txtField: UITextField!
@IBAction func txtField_EditingChanged(sender: AnyObject) {
if txtField.text?.characters.count > 0{
btn.enabled = true
}
else{
btn.enabled = false
}
}