2011-08-20 45 views
3

我有一個UITextView,我實現了一個keyboardWasShownMethod像這樣:keyboardWasShown方法不叫上我的UITextView

(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中連接。這可能是什麼原因?

回答

8

您是否添加了觀察者UIKeyboardWillShowNotification

[[NSNotificationCenter defaultCenter] 
        addObserver:self 
         selector:@selector(keyboardWasShown:) 
          name:UIKeyboardWillShowNotification 
          object:nil]; 
+0

我用這個解決方案努力,但如果我把自己的最後一個參數並沒有奏效。設置零它的工作就像一個魅力。 –

+0

@Mithu,對不起。那是我的錯誤。這應該是零或textView實例。謝謝。 – EmptyStack

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