我有个奇怪的问题,我不明白。我有一个带有几个uitextfield对象的uiscrollView。当我切换到视图时,我将第一个UIExtField设置为FirstResponder,并且由于该视图注册的uikeyboarddidshowNotification,keyboardwasshown方法被调用。奇怪的是,当我触摸下一个uitextfield时,没有调用keyboardwasshown方法。我不理解这一点,因为苹果公司的文档中说,“如果你的界面有多个文本字段,用户可以点击它们之间的内容来编辑每个字段中的值。然而,当这种情况发生时,键盘不会消失,但系统仍然会在每次编辑开始时在新的文本字段中生成uikeyboarddidshownotification通知。“我的代码也直接从Apple的文档中复制,它工作正常,但它只会被调用。”第一次。我错过了什么?
- (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 {
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];
}