【问题标题】:Iphone DelegationiPhone代表团
【发布时间】:2010-12-06 07:14:29
【问题描述】:

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

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

}

【问题讨论】:

  • 为什么不直接创建 UIViewController 的子类?

标签: iphone delegation event-delegation


【解决方案1】:

这些方法将为 UIViewController 类执行,所以我认为您可以将这些方法包装为 UIViewController 类别。

@interface UIViewController (Ext_Keyboard)

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

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

@end 

【讨论】:

  • 如果这个答案对你有帮助,你可以投票或点击答案的复选标记。
猜你喜欢
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多