iphone - 未触发 UIKeyboardWillHide

标签 iphone ios notifications uikeyboard

我在这里阅读了很多关于这个主题的帖子,但我找不到我的问题的答案,所以,希望你不会对另一篇 UIKeyboard 帖子感到厌烦:-)

在我的 View Controller 的实现中,我添加了 self 作为两个通知 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 的观察者,传递了选择器 keyboardWillShow :keyboardWillHide: 来处理通知。当我触摸 UITextField 时,keyboardWillShow: 方法被调用,但是当我按下“完成”按钮(关闭键盘)时,keyboardWillHide: 方法未被调用。

真的,我想让我的 UITextField 显示键盘右下角带有“隐藏按钮”的键盘,但我找不到正确的键盘类型。也许我需要将文本字段 retuntype 设置为“...完成”。这样我就看到“返回”键变成了“完成”。

因此,我将工具栏设置为我的 UITextFieldinputAccessoryView,因此现在我可以显示一个标准键盘,上面有一个工具栏,并带有“完成”按钮。当用户触摸该按钮时,我使用 resignFirstResponder 方法隐藏键盘。

奇怪的是,当我调用 resignFirstResponder 时,UIKeyboardWillHideNotification 没有发布;至少 keyboardWillHide: 方法没有被调用。

你对我有什么建议?我真的很想用带有向下箭头的小按钮显示键盘以隐藏键盘,但这个解决方案也可能是正确的,但我想调整 View 的大小并为此我需要观察者 UIKeyboardWillHideNotification.

非常感谢您的帮助...

(已添加:)

viewDidLoad中:

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

我从“你的”帖子中获取了这些声明 :-) 但是 willShow 有效......

UIToolbar 中的“完成”按钮的操作被指定为我的文本字段的 inputAccessoryView 是:

-(void)keyboardDone {
    [msgTextField resignFirstResponder];

关闭: 好的!当一个开发者是愚蠢的...它是愚蠢的 :-) :-)

这是我更正后的 willHide 方法:

-(void)keyboardWillHide:(NSNotification*)n {
    NSDictionary* userInfo;
    CGSize keyboardSize;
    CGRect viewFrame;

    /* This was the bad guy :) I forgot to delete it
     * after I previously copied the willShow method that
     * checks if keyboard is already shown (if so returns).
     *
     * if( keyboardIsShown )
     *   return;
     */
    userInfo = [n userInfo];
    keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    viewFrame = [[self scrollView] frame];
    viewFrame.size.height += ( keyboardSize.height - TABBAR_HEIGHT );

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];
    [[self scrollView] setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
    NSLog(@"HIDE\n");
}

首先,我要感谢大家对我的无用工作的帮助。我想给你一些分数,所以我会尝试为每个答案增加一个“兴趣点”,但我需要选择正确的……困难的部分……:-)

再次打扰一下...我真的没看到if()语句...

最佳答案

如果您阅读了 UIWindow 的文档,它说这些通知的通知对象是 nil。您正在将 self.view.window 作为对象传递给 addObserver:selector:name:object: 方法。尝试传递 nil 代替:

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

关于iphone - 未触发 UIKeyboardWillHide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12322137/

相关文章:

android - 如何通过单击通知关闭我的应用程序的任何 Activity ?

android - 在 InboxStyle 通知的右下角添加图标

iphone - 自定义 UINavigationController UINavigationBar

ios - 获取 UIView 屏幕截图的最快方法

ios - 错误未找到移动配置文件

ios - 无法调用非函数类型的值 'AWSSNS'

java - 应用程序关闭时如何获得android通知

ios - RxSwift 不能与 UIButton 一起使用

iphone - 如果我撤销我的开发者和分发,它会影响我的 Live 应用程序吗?

ios - block 类型冲突