2009-12-11 37 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个视图控制器做

希望这有助于。