我正在尝试实现一个UITextFieldDelegate
,当用户按下return时会执行某个操作。但是,虽然我从故事板和viewDidLoad
方法设置了委托,但从不调用委托方法。这是我的代码:
/*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
}
答案 0 :(得分:0)
我明白了......它真的很蠢。
在textFieldShouldEndEditing