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

Posted by Shawn on Stack Overflow See other posts from Stack Overflow or by Shawn
Published on 2010-04-15T01:58:50Z Indexed on 2010/04/15 2:03 UTC
Read the original article Hit count: 515

Filed under:

I'm having a strange problem that I don't understand. I have a UIScrollView with several UITextField objects. When I switch to the view, I set the first UITextField as firstresponder, and the keyboardWasShown method gets called due to the UIKeyboardDidShowNotification that the view is registered for. The weird thing is, when I touch the next UITextField, the keyboardWasShown method is not called. I don't understand this, since Apple's documentation says "If your interface has multiple text fields, the user can tap between them to edit the values in each one. When that happens, however, the keyboard does not disappear but the system does still generate UIKeyboardDidShowNotification notifications each time editing begins in a new text field." My code I've copied directly from Apple's documentation as well, and it works properly, but it only gets called 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];
}

© Stack Overflow or respective owner

Related posts about iphone