开发者

What is the reason the UIKeyBoardWIllShowNotification called once?

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

I have a UITabBar application with six tabs.

In each view I am using the UINavigationController.

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 .

The code is as follows :

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasHidden:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

and the methods

- (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 keyboardSi开发者_运维问答ze = [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;
}


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

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;

will throw the bad excess exceptions

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

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜