Iphone代表团

时间:2010-12-06 07:14:30

标签: iphone delegation event-delegation

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

- (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)动画{

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

}

1 个答案:

答案 0 :(得分:0)

这些方法将针对UIViewController类执行,所以我认为你可以通过UIViewController类包装这些方法。

@interface UIViewController (Ext_Keyboard)

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

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

@end