iPad mini上无法接收“UIKeyboardWillShowNotification”

时间:2012-12-25 04:26:49

标签: ios nsnotificationcenter ipad-mini

我在主视图控制器中将以下代码添加到viewWillAppear:animated

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

而且,我在同一个类中实现了这个方法,

- (void)showKeyboard:(NSNotification *)notification
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Keyboard will appear." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

主视图控制器有一个UITextField对象。

在iPad2(iOS 5.0)中,聚焦时会显示警报视图。 但是,在iPad mini(iOS 6.0)中,除软件键盘外不会显示任何内容。

我想让iPad mini与iPad2的行为相同。

谢谢,

1 个答案:

答案 0 :(得分:2)

从iOS 3.2开始,在两个文本字段之间切换时,不再触发UIKeyboardWillHideNotificationUIKeyboardWillShowNotification。基本上,只有在实际显示或隐藏键盘时才会触发通知。请改用UIKeyboardDidShowNotification

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];     
    } else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];