2009-12-11 42 views
2

我正在使用keyboardWasShownkeyboardWillBeHidden通知來滑動視圖以獲取可見的文本視圖。UIKeyBoardWIllShowNotification調用一次的原因是什麼?

我有一個UITabBar應用程序與六個選項卡。

在每個視圖中,我使用UINavigationController

在每個UITableViewCell的詳細視圖中,我正在使用鍵盤通知。

所以問題是,我第一次使用鍵盤通知。在其他選項卡上它不會工作。

的代碼如下:

[[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]; 
     NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; 
     CGSize keyboardSize = [aValue CGRectValue].size; 

     NSTimeInterval animationDuration = 0.300000011920929; 
     CGRect frame = self.view.frame; 
     frame.origin.y -= keyboardSize.height-100; 
     frame.size.height += keyboardSize.height-100; 
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
     [UIView setAnimationDuration:animationDuration]; 
     self.view.frame = frame; 
     [UIView commitAnimations]; 

    viewMoved = YES; 

    keyboardShown = YES; 
} 
- (void)keyboardWasHidden:(NSNotification *)aNotification { 
    if (viewMoved && tvAddreview) { 
     NSDictionary *info = [aNotification userInfo]; 
     NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; 
     CGSize keyboardSize = [aValue CGRectValue].size; 

     NSTimeInterval animationDuration = 0.300000011920929; 
     CGRect frame = self.view.frame; 
     frame.origin.y += keyboardSize.height-100; 
     frame.size.height -= keyboardSize.height-100; 
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 
     [UIView setAnimationDuration:animationDuration]; 
     self.view.frame = frame; 
     [UIView commitAnimations]; 

     viewMoved = NO; 
    } 

    keyboardShown = NO; 
} 
+0

您也可以從通知的userInfo中獲取animationDuration值。 – Morion 2009-12-11 13:22:59

+0

這些方法和觀察者添加位置在哪裏? – Morion 2009-12-11 13:24:33

+0

我在viewDidLoad – harshalb 2009-12-12 06:29:02

回答

9

你應該eachClass dothis這樣的:

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

因爲通知是在應用程序級別不在您的課程級別。所以,如果你將它們添加到一個班級而不是所有班級,那麼就去下一堂課。該通知仍然會調用密鑰keyboardWasShown:,另一個來自您添加通知的類,因此您的本地變量如... viewMoved = YES;

keyboardShown = YES; 

將拋出壞過剩例外

在你的情況下,還需要在所有6個視圖控制器做

希望這有助於。