2010-08-16 50 views
4

我正在使用一种技巧将键盘与自己的覆盖。基本上,我将一些按钮添加为键盘的子视图。这是我如何找到键盘的UIView:如何在iPhone,iOS 3.2或更高版本上获取键盘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。

在此先感谢

回答

3

我说只是用这个[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];

- (void)keyboardWillShow { [self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0]; }

这是在键盘出现时调用的。

+0

它的工作,虽然我觉得这可能会造成麻烦。这只是一种感觉,但我认为延迟:0可能会太快地调用选择器......但是,它很有效。 最后,我不得不同时使用:keyboardWillShow,延迟键盘显示覆盖和keyboardDidShow让它保持那样。不知怎的,一旦视图完全加载,叠加层就消失了。 但无论如何,它的工作。谢谢。 – Bujtor 2010-08-16 14:37:13

+0

使用performSelector时:afterDelay:主线程正在处理调用,并且由于uikeyboard动画由主线程调用,所以您必须等待它完成,因此请使用performSelector:afterDelay: – Simon 2010-09-03 08:27:00

相关问题