2012-05-09 4 views
0

나는 어디서나 찾고있었습니다 ... 아마도 나는 이것이 일반적인 질문이라고 믿기 때문에 올바른 검색 단어를 사용하지 않고 있습니다.키보드 버튼을 클릭하여 화면 키보드를 닫는 이벤트

사용자가 키보드를 낮추기 위해 단추를 클릭하여 키보드를 닫을 때 처리 할 수있는 이벤트가 있습니까? 는, UITextField가 firstresponder 될 때

The red marked button

은 내가보기 위로 이동하지만,이 버튼은 "관리 키보드"절에있는 두 번째 단락을

답변

0

알림을 사용해보세요.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

을 다음 keyboardWillHide라는 방법 작성 : 당신의 viewDidLoad에 그 추가

- (void)keyboardWillHide:(NSNotification *)notification { 
    //do whatever you need 
} 

그것을 사용하여

0

희망이 도움을 NSNotificationCenter 당신은 events.You이 키보드 이벤트를 등록 할 수 있습니다 키보드를 얻을 수 viewWillAppear 및 viewWillDisapper 등록을 잊지 마세요.
우리는 여기에 사용이 통지 :

  • (무효) viewWillAppear : 당신은 다음과 같은 몇 가지 일을 할 수 apple docs

    1. UIKeyboardDidShowNotification apple docs
    2. UIKeyboardDidHideNotification (BOOL)는 애니메이션 {
      [super viewWillAppear : animated];
      NSLog (@ "키보드 이벤트 등록"); 이벤트에 대한

// 등록
[[NSNotificationCenter defaultCenter] addObserver : 자기 선택 : @selector (keyboardDidShow : 이름 : UIKeyboardDidShowNotification 대상 : 전무]
[NSNotificationCenter defaultCenter] addObserver : 자기 선택기 : @ 선택기 (keyboardDidHide :) 이름 : UIKeyboardDidHideNotification 개체 : 닐];

// 설치 콘텐츠 크기 scrollview.contentSize = CGSizeMake (SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT); // 대한 예 (320,460)

// 초기에는 키보드가 숨겨져 있습니다. keyboardVisible = NO; // in.h 선언 BOOL keyboardVisible; }

- (무효) viewWillDisappear : {
NSLog ("키보드 이벤트에 대한 등록 취소"@) 애니메이션 (BOOL);
[[NSNotificationCenter defaultCenter] removeObserver : self];
}

- (무효) keyboardDidShow (NSNotification *) NOTIF {
NSLog (@ "키보드가 표시되는");
// 키보드가 보이는 경우
if (keyboardVisible) {
NSLog (@ "키보드가 이미 표시됩니다. 알림을 무시하십시오.");
return;
}

// Get the size of the keyboard. 
NSDictionary* info = [notif userInfo]; 
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; 
CGSize keyboardSize = [aValue CGRectValue].size; 

// Save the current location so we can restore 
// when keyboard is dismissed 
offset = scrollview.contentOffset; //in .h declare CGPoint offset and UIScrollView *scrollview.; 

// Resize the scroll view to make room for the keyboard 
CGRect viewFrame = scrollview.frame; 
viewFrame.size.height -= keyboardSize.height; 
scrollview.frame = viewFrame; 

CGRect textFieldRect = [activeField frame];//in .h UITextField *activeField; 
textFieldRect.origin.y += 10; 
[scrollview scrollRectToVisible:textFieldRect animated:YES];  

// Keyboard is now visible 
keyboardVisible = YES; 

}

- (무효) keyboardDidHide : (! keyboardVisible) {
// 이미 표시된 키보드가 터들하고 싸우는데 (NSNotification *)
경우 {
NSLog (@ "키보드가 이미 숨겨져 있습니다. 알림을 무시하십시오.");
return;
}

// Reset the frame scroll view to its original value 
scrollview.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT); 

// Reset the scrollview to previous location 
scrollview.contentOffset = offset; 

// Keyboard is no longer visible 
keyboardVisible = NO; 

}

- (BOOL) textFieldShouldReturn (의 UITextField *)에 textField
{
[에 textField resignFirstResponder];
return 예;
}
희망이 도움이 될 것입니다.

관련 문제