I have a UITextView and I implemented a keyboardWasShownMethod like so:
我有一个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");
}
But for some reason it's not getting called when my keyboard appears. This method is in the same class as the UITextView and the UITextView is declared in the .h file and connected in XIB. What could be the cause of this?
但出于某种原因,当我的键盘出现时,它不会被调用。此方法与UITextView位于同一个类中,UITextView在.h文件中声明,并在XIB中连接。这可能是什么原因造成的呢?
2 个解决方案
#1
8
Did you add the observer for UIKeyboardWillShowNotification?
你添加了UIKeyboardWillShowNotification的观察者了吗?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
#2
3
Turned out it was because I didn't have these in my code:
因为我的代码里没有这些
-(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];
}
#1
8
Did you add the observer for UIKeyboardWillShowNotification?
你添加了UIKeyboardWillShowNotification的观察者了吗?
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
#2
3
Turned out it was because I didn't have these in my code:
因为我的代码里没有这些
-(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];
}