4

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];
}

1 Answer 1

1

This notification is not posted every time you change first responders. For that, you must implement the delegate protocols for either UITextField or UITextView (depending on what you are using) and deal with these changes from there. You only get the notification that the keyboard was displayed if the keyboard was previously hidden.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm implementing the textFieldDidBeginEditing and textFieldDidEndEditing delegate methods, but I don't understand how to deal with the changes in those methods without the UINotification generated by the system. How do I know where the keyboard is?
What i've done is add an instance variable that I set on receipt of keyboard did show and clear on receipt of keyboard did hide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.