我有一个UITextView,我实现了一个keyboardWasShownMethod,如下所示:
(void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = inkTextField.superview.frame;
bkgndRect.size.height += kbSize.height;
[inkTextField.superview setFrame:bkgndRect];
[inkScroller setContentOffset:CGPointMake(0.0, inkTextField.frame.origin.y-kbSize.height) animated:YES];
inkTextField.frame=CGRectMake(1, -5, 285, 221);
NSLog(@"Called keyBoardWasShwon");
}
但由于某种原因,当我的键盘出现时它不会被调用。此方法与UITextView位于同一类中,UITextView在.h文件中声明并在XIB中连接。可能是什么原因造成的?
答案 0 :(得分:8)
您是否为 UIKeyboardWillShowNotification 添加了观察者?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
答案 1 :(得分:3)
原来是因为我的代码中没有这些:
-(void) viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWasHidden:) name: UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear: (BOOL)animated{
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name: UIKeyboardWillShowNotification object:nil];
[nc removeObserver:self name: UIKeyboardWillHideNotification object:nil];
}