2010-12-06 6 views
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]; 

} 경우 매우 감사하게 될 것입니다 - (무효) viewWillDisappear : (BOOL)이 애니메이션 {

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

}

+0

왜 UIViewController의 하위 클래스를 만듭니 까? – nduplessis

답변

0

이 방법의 UIViewController 클래스에 대한 실행됩니다, 그래서 생각 UIViewController Category에 의해이 메소드들을 래핑 할 수 있습니다.

@interface UIViewController (Ext_Keyboard) 

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

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

@end 
+0

아하, 도와 줘서 고마워. – zed111

+0

이 답변이 도움이된다면, 투표에 응답하거나 답을 확인 표시 할 수 있습니까? – AechoLiu