UIKeyboardWillHide未触发 [英] UIKeyboardWillHide not triggered

查看:179
本文介绍了UIKeyboardWillHide未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了很多有关该主题的文章,但是我找不到我的问题的答案,因此,希望您不会对另一篇UIKeyboard文章感到厌烦:-)

I read many post here about this topic, but I wasn't able to find an answer to my question, so, hope you won't be bored about another UIKeyboard post :-)

在我的视图控制器的实现中,我添加了self作为两个通知UIKeyboardWillShowNotificationUIKeyboardWillHideNotification的观察者,并将选择器keyboardWillShow:keyboardWillHide:传递给通知.当我触摸UITextField时,将调用keyboardWillShow:方法,但是当我按下完成"按钮(关闭键盘)时,不会调用keyboardWillHide:方法.

In my view controller's implementation I added self as an observer for the two notifications UIKeyboardWillShowNotification and UIKeyboardWillHideNotification, passing the selectors keyboardWillShow: and keyboardWillHide: to handle to notifications. As I touch a UITextField, the keyboardWillShow: method is called but when I press a "Done" button (which dismisses the keyboard) the keyboardWillHide: method is not called.

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

Really, I'd like to make my UITextField show a keyboard with the "hide button" on the bottom right of the keyboard, but I wasn't able to find the right keyboard type. Maybe I need to set the textfield retuntype to "...Done". In that way I saw that "return" key turns to "done".

因此,我将工具栏设置为UITextFieldinputAccessoryView,因此现在可以通过完成"按钮在上方显示带有工具栏的标准键盘.当用户触摸该按钮时,我使用resignFirstResponder方法隐藏了键盘.

So I set a toolbar to be my UITextField's inputAccessoryView, so now I can show a standard keyboard with a tool bar above with the "Done" button. As a user touches that button, I hide the keyboard with the resignFirstResponder method.

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

The strange thing is that when I call resignFirstResponder, the UIKeyboardWillHideNotification isn't posted; at least the keyboardWillHide: method is not called.

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

What do you suggest to me? I really wanted to display a keyboard with the small button with the down arrow to hide the keyboard, but also this solution could be right, but I'd like to resize the view and to do this I need to observer UIKeyboardWillHideNotification.

非常感谢您的帮助...

Thank you very much for help...

(添加:)

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可以工作...

I took these declarations from one of "yours" post :-) But the willShow works...

分配给我的文本字段inputAccessoryViewUIToolbar中的完成"按钮的操作是:

The action of the "Done" button that's in the UIToolbar that's assigned to be the inputAccessoryView of my text field is:

-(void)keyboardDone {
    [msgTextField resignFirstResponder];

关闭: 好的!当开发人员是愚蠢的...这是愚蠢的:-) :-)

CLOSED: OK! When a developer is stupid... it is stupid :-) :-)

这是我更正的willHide方法:

This is my corrected willHide method:

-(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");
}

首先,我要感谢你们在帮助我方面所做的无用工作.我想给您一些要点,所以我将尝试为每个答案提高一个兴趣点",但是我需要选择正确的一个...难的部分... :-)

First of all I'd like to thank you all for this useless work in helping me. I'd like to give you some points, so I'll try to rise a "interest point" for each answer, but I need to choose the right one... hard part... :-)

再次对不起...我真的没看到if()语句...

Excuse me again... I really didn't see the if() statement...

推荐答案

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

If you read the documents for UIWindow it says that the notification object for these notifications is nil. You are passing self.view.window in as the object to the addObserver:selector:name:object: method. Try passing nil instead:

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

这篇关于UIKeyboardWillHide未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆