2011-09-03 10 views
6

여기에 문제가 있습니다. 프로그램을 디버깅 할 때마다 keyboardWillShow 함수가 매번 응답하지 않습니다. 단지 처음에는 프로그램에 의해 호출됩니다. 여기 내 코드입니다, 내 코드에서 뭐가 잘못 됐는지 모르지만, 키보드가 처음 나타 났을 때 함수는 잘 돌아갑니다.keyboardWillShow가 응답하지 않습니다.

- (void)keyboardWillShow:(NSNotification *)notification { 

    /* 
    Reduce the size of the text view so that it's not obscured by the keyboard. 
    Animate the resize so that it's in sync with the appearance of the keyboard. 
    */ 


    NSDictionary *userInfo = [notification userInfo]; 

    // Get the origin of the keyboard when it's displayed. 
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. 
    CGRect keyboardRect = [aValue CGRectValue]; 
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; 

    CGFloat keyboardTop = keyboardRect.origin.y; 
    CGRect newTextViewFrame = self.textview.frame; 

    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y; 

    // Get the duration of the animation. 
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval animationDuration; 
    [animationDurationValue getValue:&animationDuration]; 

    // Animate the resize of the text view's frame in sync with the keyboard's appearance. 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animationDuration]; 

    textview.frame = newTextViewFrame; 

    [UIView commitAnimations]; 
} 
- (void)keyboardWillHide:(NSNotification *)notification { 

    NSDictionary* userInfo = [notification userInfo]; 

    /* 
    Restore the size of the text view (fill self's view). 
    Animate the resize so that it's in sync with the disappearance of the keyboard. 
    */ 
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval animationDuration; 
    [animationDurationValue getValue:&animationDuration]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:animationDuration]; 

// textview.frame = self.view.bounds; 
    [self save]; 

    [UIView commitAnimations]; 
} 

와 나는 REGIST 통지

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

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

} 

내가 firstresponder을 사임

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    [self save]; 
    self.textview = nil; 
    self.title = nil; 
    self.tags = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 


} 

여기에서 그것을 제거, 여기

- (IBAction)save:(id)sender { 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNote)] autorelease]; 

    [textview resignFirstResponder]; 


} 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(save:)] autorelease]; 
    [self setUpUndoManager]; 

    return YES; 


} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ 
    [self save]; 
    return YES; 

} 
+1

당신은 더 많은 정보가 필요합니까, 첫 번째 응답을 릴리스합니까? 무슨 일이 일어나고 두 번째 등 –

+0

예, 나는 FirstResponder를 사임, 여기 내 코드입니다 - (BOOL) textFieldShouldBeginEditing : (UITextField *) textField { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem : UIBarButtonSystemItemDone target : 자기 행동 : @selector (save :)] autorelease]; [self setUpUndoManager]; [textField resignFirstResponder]; return 예; - (BOOL) textFieldShouldEndEditing : (UITextField *) textField { [자동 저장]; [textField resignFirstResponder]; return YES; } – leoyfm

+0

키보드가 두 번째로 표시 될 때 keyboardWillShow 메서드가 자동으로 호출되지 않습니다. 즉 keyboardWillShow 메서드는 키보드 팝업이 처음 실행될 때만 실행됩니다. 그 후에이 방법은 다시는 실행되지 않습니다. – leoyfm

답변

5

것은 당신이 있는지 확인 내 코드입니다 ob를 제거하는 코드를 작성하지 않는다. 서버 ..... keyboardWillHide 방법을 제공하십시오 ....

+0

viewDidUnload 메서드에서 observer를 제거합니다. 사실, 내가 그것을 제거하지 않더라도, 문제는 여전히 존재합니다. – leoyfm

+0

viewDidUnload에서 관찰자를 제거하는 것은 멋지다 ... 나는 한 가지를 이해하지 못한다. 왜 [textField resignFirstResponder]를 작성 했는가? in textFieldShouldBeginEditing ... 그것에 대해 조금 더 ..... ..... – Mohammad

+0

약간 이상 할지도 모릅니다. 사용자가 텍스트 필드를 탭하면 키보드가 사라지도록하고 싶습니다. 그러나 나는 키보드가 나타나고 사라지는 행동 사이에 충돌이 있음을 압니다. 내 코드에서이 메서드를 제거합니다. 그러나 문제는 여전히 있습니다. 나는 왜 처음에는 모든 것이 좋지 않습니다. 텍스트 필드 스크롤은 완벽합니다. 하지만 그 이후에는 텍스트 필드가 스크롤되지 않습니다. 디버그를 통해 keyboardwillshow 메서드가 처음 실행 된 적이 없다는 것을 알았습니다. keyboardwillshow 메서드를 사용하여 텍스트 필드 프레임을 조정하는 코드입니다. 그래서 처음으로 텍스트 필드가 스크롤되지 않는 이유. – leoyfm

관련 문제