Search code examples
iosuitextfielduitextviewios7

Deadlock when changing first responder from uitextfield to uitextview [iOS 7]


have a really strange problem here, this did not happen before iOS 7...

i have a uitextfield and uitextview in a form that i created... the problem is that if the user has the textfield as first responder then taps on the uitextview a deadlock happens, memory will increase until the watchdog kills my app..

This does not happen when I change from uitextview to uitextfield

Relevant code:

#pragma mark - UITextView Delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if ([text isEqualToString:@"\n"]) {
    [textView resignFirstResponder];
}

NSUInteger newLength = [textView.text length] + [text length] - range.length;

return (newLength > 120) ? NO : YES;

}

-(void)textViewDidEndEditing:(UITextView *)textView {

if (textView.tag == CreatePlaceElementDescription) {
    self.marker.info = textView.text;
}
else if (textView.tag == CreatePlaceElementAddress) {
    self.marker.address = textView.text;
}
}

#pragma mark - UITextField Delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

if ([string isEqualToString:@"\n"]) {
    [textField resignFirstResponder];
}

NSUInteger newLength = [textField.text length] + [string length] - range.length;

//Limit name textfield length
return (newLength > 60) ? NO : YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {

if (textField.tag == CreatePlaceElementName) {
    self.marker.name = textField.text;
}

}

There is nothing more to this than that...

if i resign first responder first this problem won't happen but it will make the user tap the textview twice and that is undesired..

Also the deadlock happens on textview:didEndEditing, (as if the textview was the one resigning the keyboard not the textfield, textfield:didEndEditing is also called).. textview:didEndEditing should not be called anywhere

it really boggles my mind... any suggestions?


Solution

  • Ok i got what's the problem

    i'm using DaKeyboardControl to adjust views when the keyboard appears... what's odd is that it seems this is broken on iOS 7 when changing first responders (it won't enter the deadlock when only one textview/textfield is present)... i'm opening a BUG report to their githubs while i figure which line is producing this error... when i have it i'll share it on a EDIT with you

    EDIT: The problem is on the UIKeyboardWillShowNotification receivers... this notification gets called multiple times... the solution it seems is to use UIKeyboardDidChangeFrameNotification or UIKeyboardWillChangeFrameNotification to perform the change of frame...

    I hope this can help someone... don't know if the use of UIKeyboardWillShowNotification will present problems to people not using iOS 7 now