0

嗨,我正在使用以下方法来提升键盘,我有许多视图控制器也可以使用它,但我尝试委派它失败了。我绝对不想将它插入到每个视图控制器中。如果有任何想法将不胜感激

- (void)viewWillAppear:(BOOL)animated {

void (^keyBoardWillShow) (NSNotification *)= ^(NSNotification * notif) {
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;  
    float bottomPoint = (ivcTextField.frame.origin.y + ivcTextField.frame.size.height + 10);
    scrollAmount = keyboardSize.height - (self.view.frame.size.height - bottomPoint);

    if (scrollAmount > 0)  {
        moveViewUp =YES;
        [self scrollTheView:YES];
    }
    else
        moveViewUp = NO;
};

[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:self.view.window queue:nil 
                                              usingBlock:keyBoardWillShow];

void (^keyBoardWillHide) (NSNotification *)= ^(NSNotification * notif) {
    if (moveViewUp) [self scrollTheView:NO];    
};
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:self.view.window queue:nil 
                                              usingBlock:keyBoardWillHide]; 

[super viewWillAppear:animated];  

} - (void)viewWillDisappear:(BOOL)animated {

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
[super viewWillDisappear:animated];

}

4

1 回答 1

0

这些方法将为 UIViewController 类执行,因此我认为您可以通过 UIViewController 类别包装这些​​方法。

@interface UIViewController (Ext_Keyboard)

- (void)registerObserverForKeyboardDidShow;
- (void)unregisterObserverForKeyboardDidShow;

- (void)registerObserverForKeyboardDidHide;
- (void)unregisterObserverForKeyboardDidHide;

@end 
于 2010-12-06T07:59:23.473 回答