2011-09-03 34 views
6

đây là sự cố, khi tôi gỡ lỗi chương trình của mình, tôi thấy chức năng keyboardWillShow không phản hồi mọi lúc. chỉ lần đầu tiên, nó sẽ được gọi bởi chương trình. đây là mã của tôi, tôi không biết whats sai trong mã của tôi, nhưng, khi bàn phím đầu tiên xuất hiện, chức năng chạy tốt.keyboardWillShow không trả lời

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

và tôi regist thông báo

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

} 

và loại bỏ nó ở đây

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


} 

tôi từ chức firstresponder, đây là mã của tôi

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

Bạn cần thêm thông tin, bạn có trả lời tự động đầu tiên không? Điều gì đang xảy ra lần thứ hai vv –

+0

có, tôi từ chức FirstResponder, đây là mã của tôi - (BOOL) textFieldShouldBeginChỉnh sửa: (UITextField *) textField { self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self action : @selector (lưu :)] autorelease]; [self setUpUndoManager]; [textField resignFirstResponder]; trả lại CÓ; } - (BOOL) textFieldShouldEndChỉnh sửa: (UITextField *) textField { [tự lưu]; [textField resignFirstResponder]; trả lại CÓ; } ' – leoyfm

+0

khi bàn phím hiển thị trong lần thứ hai, phương pháp keyboardWillShow không được gọi tự động. nói cách khác, phương thức keyboardWillShow chỉ chạy ở lần đầu tiên khi bàn phím bật lên. sau đó, phương pháp này không bao giờ chạy lại. – leoyfm

Trả lời

5

Hãy chắc chắn rằng bạn đang không viết bất kỳ mã nào để xóa ob máy chủ ..... Vui lòng cung cấp phương thức bàn phímWillHide ....

+0

tôi xóa người quan sát trong phương thức viewDidUnload. trong thực tế, mặc dù tôi không loại bỏ nó, vấn đề vẫn còn tồn tại. – leoyfm

+0

loại bỏ quan sát trong viewDidUnload là mát mẻ .... Tôi không hiểu một điều ... tại sao bạn đã viết [textField resignFirstResponder]; trong textFieldShouldBeginChỉnh sửa ... cung cấp thêm một chút về nó ..... – Mohammad

+0

nó có thể hơi lạ. tôi chỉ muốn làm cho bàn phím biến mất khi người dùng nhấn vào trường văn bản. nhưng tôi biết có một xung đột giữa bàn phím xuất hiện và biến mất hành động. tôi xóa phương thức này khỏi mã của tôi. nhưng vấn đề vẫn còn. Tôi không lý do tại sao trong lần đầu tiên, mọi điều đều tốt. di chuyển textfield là hoàn hảo. nhưng sau đó, trường văn bản không cuộn. tôi thấy rằng thông qua gỡ lỗi, phương pháp keyboardwillshow không bao giờ chạy sau lần đầu tiên. mã để điều chỉnh khung văn bản mà tôi viết bằng phương pháp keyboardwillshow. sao cho trường văn bản không cuộn sau lần đầu tiên. – leoyfm

Các vấn đề liên quan