I am trying to implement a UITextFieldDelegate
that performs a certain thing when the user presses return. However, although I set the delegate both from the storyboard and from the viewDidLoad
method, the delegate methods are never called. Here is my code:
/*UITextFieldDelegate*/
@IBOutlet weak var messageTextField: UITextField!
func textFieldDidBeginEditing(textField: UITextField) { //delegate method
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
//check if message isn't empty
if let _ = textField.text?.isEmpty {
} else {
chatMessages.append(ChatMessage(message: (textField.text)!, dateSent: NSDate()))
textField.text = ""
//reload table data
tableView.reloadData()
}
return true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
messageTextField.delegate = self
}
I figured it out... Its really dumb.
Just return true instead of false in textFieldShouldEndEditing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.