Forced UIView EndEditing Not Working As Expected

2019-05-28 21:44发布

问题:

I was using the -(BOOL)endEditing:(BOOL)force method in a UIView to end editing of the textfields in the view and discovered some unexpected behavior.

I created a simple test program, with a UITextField and two UIButtons. One button calls [self.view endEditing:YES] to force editing to end, while the other calls [self.view endEditing:NO] to end editing, without forcing.

The File is the textfield's delegate and I have put a simple logging statement in the -(BOOL)textFieldShouldEndEditing:(UITextField *)textField method.

Here's the code for the buttons and the text field delegate:

-(IBAction)doEndEditingForced:(id)sender {
    NSLog(@"==========================");
    NSLog(@"In doEndEditingForced");
    [self.view endEditing:YES];
}

-(IBAction)doEndEditingNotForced:(id)sender {
    NSLog(@"==========================");
    NSLog(@"In doEndEditingNotForced");
    [self.view endEditing:NO];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"ShouldEndEditing");
    return YES;
}

And here's the output I'm getting:

2011-10-03 15:36:22.153 Test[94596:207] ==========================
2011-10-03 15:36:22.155 Test[94596:207] In doEndEditingForced
2011-10-03 15:36:22.156 Test[94596:207] ShouldEndEditing
2011-10-03 15:36:26.145 Test[94596:207] ==========================
2011-10-03 15:36:26.146 Test[94596:207] In doEndEditingNotForced
2011-10-03 15:36:26.147 Test[94596:207] ShouldEndEditing
2011-10-03 15:36:26.148 Test[94596:207] ShouldEndEditing

In the case of the call to endEditing with the force parameter equal to YES, why is the textFieldShouldEndEditing method being called at all, and in the case of the call to endEditing with the force parameter equal to NO, why is the textFieldShouldEndEditing method being called twice?

回答1:

That happen because endEditing methods dont call any delegate methods for your UITextField or UITextView is just just hide keyboard from your view.



标签: uiview