2010-12-06 18 views
0

嗨 我使用以下來提高鍵盤,我有很多視圖控制器也可以使用它,但我的嘗試委派失敗。我絕對不希望將其插入到每個視圖控制器中。如果任何想法Iphone代表

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

} 將不勝感激 - (空)viewWillDisappear:(BOOL)動畫{

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

}

+0

爲什麼不直接創建UIViewController的子類? – nduplessis 2010-12-06 08:17:43

回答

0

這些方法將執行的UIViewController類,所以我覺得你可以通過UIViewController Category來包裝這些方法。

@interface UIViewController (Ext_Keyboard) 

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

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

@end 
+0

啊哈,謝謝你的幫助。 – zed111 2010-12-07 04:17:41