iphone - KeyBoardWIllShowNotification 调用一次的原因是什么?

标签 iphone notifications

我正在使用 keyboardWasShownkeyboardWillBeHidden 通知来滑动 View 以获取可见的 TextView 。

我有一个带有六个选项卡的 UITabBar 应用程序。

在每个 View 中我都使用 UINavigationController

在每个 UITableViewCell 的详细 View 中,我使用键盘通知。

所以问题是键盘通知在我第一次使用时起作用。在其他选项卡上打开它将不起作用。

代码如下:

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

最佳答案

你应该在每个类中这样做:

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

因为通知是在应用程序级别而不是您的类(class)级别。因此,如果您已将它们添加到一个类中而不是所有类中,则转到下一类。通知仍会调用键 keyboardWasShown: 以及您添加通知的类中的另一个键,因此您的本地变量如... View 移动=是;

keyboardShown = YES;

会抛出坏的多余异常

在您的情况下,还需要在所有 6 个 View Controller 中执行此操作

希望这有帮助。

关于iphone - KeyBoardWIllShowNotification 调用一次的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887891/

相关文章:

iphone - ViewController长度无法识别的选择器已发送到iPhone中的实例

iphone - 核心图折线图 : Add padding to the right of the graph

ios - 如果 OSX/iOS 中的网络代理设置发生变化,有没有办法接收通知/回调?

android - 服务结束时发出通知声

ios - 当我使用几种不同的方式显示单元格时,是否可以在 UITableViewCell 中有不同的高度?

iphone - 如何检查核心数据中的实体是否具有值 id?

iOS 应用提交 : invalid signature

javascript - 服务器发送事件 : Not working

iphone - iPhone 应用程序中基于计时器的提醒

c# - 以编程方式在 Windows 10 中切换焦点辅助模式