开发者

Iphone Delegation

开发者 https://www.devze.com 2023-01-28 20:31 出处:网络
Hi I\'m using the following to raise the keyboard, I have many view controllers that could also use it but my attempts to delegate it have failed. I definitely don\'t want to insert this into every v

Hi I'm using the following to raise the keyboard, I have many view controllers that could also use it but my attempts to delegate it have failed. I definitely don't want to insert this into every view controller. Would be very grateful if any ideas

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

}


These methods will execute for UIViewController class, so I think you can wrap these methods by UIViewController Category.

@interface UIViewController (Ext_Keyboard)

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

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

@end 
0

精彩评论

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