Difference between textfieldshouldendediting and textfieldDidendediting in iPhone

前端 未结 2 1286
无人及你
无人及你 2021-01-02 17:01

What is the difference between textFieldShouldendEditing and textfieldDidEndEditing, and when should each method be used?

2条回答
  •  被撕碎了的回忆
    2021-01-02 17:32

    textFieldShouldEndEditing:

    Asks the delegate if editing should stop in the specified text field.

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    

    Discussion This method is called when the text field is asked to resign the first responder status. This might occur when your application asks the text field to resign focus or when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text field calls this method to give your delegate a chance to decide whether it should.

    Normally, you would return YES from this method to allow the text field to resign the first responder status. You might return NO, however, in cases where your delegate detects invalid contents in the text field. By returning NO, you could prevent the user from switching to another control until the text field contained a valid value.

    textFieldDidEndEditing:

    Tells the delegate that editing stopped for the specified text field.

    - (void)textFieldDidEndEditing:(UITextField *)textField
    

    Discussion This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing. Implementation of this method by the delegate is optional.

    site:apple.com textFieldShouldendEditing

    textFieldShouldEndEditing

    textFieldDidEndEditing

提交回复
热议问题