- (void) keyboardWasShown is not called when switching to another UITextField

I have a strange problem that I do not understand. I have a UIScrollView with multiple UITextField objects. When I switch to the view, I set the first UITextField as the firstresponder, and the keyboardWasShown method is called because of the UIKeyboardDidShowNotification for which the view is registered. Strange, when I touch the next UITextField, the keyboardWasShown method is not called. I don’t understand this, because the Apple documentation says: “If your interface has several text fields, the user can switch between them to edit the values ​​in each of them. However, when this happens, the keyboard does not disappear, but the system still works generate UIKeyboardDidShowNotification notifications every time editing starts in a new text field. "I copied my code directly from the Apple documentation, and it works correctly, but it only receives a call for the first time. What am I missing?

- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
//if (keyboardShown) return;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame; 
bkgndRect.size.height += kbSize.height; 
[activeField.superview setFrame:bkgndRect]; 
[scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y) animated:YES]; 
keyboardShown = YES;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
}
+3
1

, . UITextField UITextView ( , ), . , , .

+1

Source: https://habr.com/ru/post/1741199/


All Articles