UIKeyBoardWIllShowNotification调用一次的原因是什么?

时间:2009-12-11 12:52:48

标签: iphone notifications

我正在使用keyboardWasShownkeyboardWillBeHidden通知来滑动视图以获取可见的文本视图。

我有一个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;
}

1 个答案:

答案 0 :(得分:9)

你应该像这样在每个类中使用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个视图控制器中执行

希望这有帮助。