Swift:永远不会设置UITextField Delegate,或者从不调用方法

时间:2015-08-05 18:11:12

标签: ios swift uitextfield

我正在尝试实现一个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
}

1 个答案:

答案 0 :(得分:0)

我明白了......它真的很蠢。

textFieldShouldEndEditing

中返回true而不是false