【问题标题】:keyboardWillShow not respond键盘将显示没有响应
【发布时间】:2011-11-09 14:43:13
【问题描述】:

这里有问题,当我调试我的程序时,我发现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];
}

我注册了通知

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

}

并在此处删除它

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

}

【问题讨论】:

  • 您需要更多信息,您会发布第一响应者吗?第二次发生了什么等
  • 是的,我辞去 FirstResponder 职务,这是我的代码 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action: @selector(save:)] 自动释放]; [自我设置UndoManager]; [textField resignFirstResponder];返回是; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ [self save]; [textField resignFirstResponder];返回是; }`
  • 第二次显示键盘时,不会自动调用keyboardWillShow方法。也就是说,keyboardWillShow 方法只是在键盘弹出的第一次运行。之后,这个方法就再也不会运行了。

标签: iphone objective-c ios4 keyboard


【解决方案1】:

确保您没有编写任何代码来删除观察者..... 也请提供keyboardWillHide方法....

【讨论】:

  • 我在 viewDidUnload 方法中移除了观察者。事实上,即使我不删除它,问题仍然存在。
  • 在 viewDidUnload 中删除观察者很酷......我不明白一件事......为什么你写了 [textField resignFirstResponder];在 textFieldShouldBeginEditing...提供更多信息.....
  • 这可能有点奇怪。当用户点击文本字段时,我只想让键盘消失。但我知道键盘出现和消失动作之间存在冲突。我从我的代码中删除了这个方法。但问题仍然存在。我不为什么在第一次,每件事都很好。文本字段滚动是完美的。但在那之后,文本字段不再滚动。我发现通过调试,keyboardwillshow 方法在第一次之后永远不会运行。我在keyboardwillshow方法中编写的调整文本字段框架的代码。所以为什么第一次后文本字段不滚动。
  • 你的答案是正确的。我再次检查了我的代码,我发现我在清理 undomanager 内容时删除了观察者。感谢您的帮助,我的程序现在可以完美运行了。
猜你喜欢
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多