UIKeyBoardWIllShowNotification调用一次的原因是什么? [英] What is the reason the UIKeyBoardWIllShowNotification called once?

查看:47
本文介绍了UIKeyBoardWIllShowNotification调用一次的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using keyboardWasShown and keyboardWillBeHidden notification for sliding the view to get the visible text view.

我有一个带有六个标签的 UITabBar 应用程序.

I have a UITabBar application with six tabs.

在每个视图中,我都使用 UINavigationController .

In each view I am using the UINavigationController.

在每个 UITableViewCell 的详细信息视图中,我正在使用键盘通知.

In the detail view of each UITableViewCell I am using the keyboard notifications.

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

So the problem is that the keyboard notifications work for the first time that I will use . on on the other tabs it won't work .

代码如下:

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

推荐答案

您应该像这样在eachClass中这样做:

you should dothis in eachClass like this:

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

because the notifications are on the application level not to your class level. So if you have added them in one class and not in all classes, then going to the next class. the notification will still call the the key keyboardWasShown: and the other from the class in which you added the notifications hence your local variables like... viewMoved = YES;

keyboardShown = YES;

将抛出严重的多余异常

根据您的情况,还需要在所有6个视图控制器中执行

In your case it is also required to do in all 6 view controllers

希望这会有所帮助.

这篇关于UIKeyBoardWIllShowNotification调用一次的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆