如何在iPhone上获取键盘UIView,iOS 3.2或更新版本

4
我正在使用一种技巧来覆盖原有的键盘。基本上,我将一些按钮添加为键盘的子视图。以下是我查找键盘的 UIView 的方法:
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(UIView* potentialKeyboard in tempWindow.subviews)
    // if the real keyboard-view is found, remember it.
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        if([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            keyboard = potentialKeyboard;
        }
        else {
            if([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                keyboard = potentialKeyboard;
    }

这个方法是由UIKeyboardDidShowNotification调用的。我更希望它被UIKeyboardWillShowNotification调用,因为“Did”版本会在键盘显示给用户后才显示。但如果我使用上面的相同过程,它会产生EXC_BAD_ACCESS错误。显然,键盘的视图没有正确找到。

现在我的问题是:有没有办法在UIKeyboardWillShowNotification触发时访问键盘的UIView。

提前致谢。

1个回答

3
我只是简单地使用了以下代码:[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];,并且在- (void)keyboardWillShow方法中添加了[self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0];。这个方法会在键盘即将出现时调用。

它起作用了,尽管我有一种感觉这可能会引起麻烦。只是一种感觉,但我认为delay:0可能会调用选择器太快...但好吧,它能工作。 最终我不得不同时使用keyboardWillShow和延迟来使键盘出现在叠加状态,并使用keyboardDidShow使其保持那样。不知何故,一旦视图完全加载,叠加层就消失了。 但无论如何,它能工作。谢谢。 - Bujtor
当使用performSelector:afterDelay:时,主线程正在处理调用,因为uikeyboard动画是由主线程调用的,所以您必须等待它完成,因此,请使用performSelector:afterDelay:。 - Simon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接