ios - iOS8键盘事件无法正确执行

标签 ios scroll keyboard

嗨,我正在开发一个iphone应用程序,在其中我使用了几个键盘事件,但是这些事件在IOS8中无法正常工作,就像在以前的IOS版本中一样。所以这是我的问题。我有两个文本字段。我正在听两个事件(void)keyboardWillBeHidden:(NSNotification *)notification(void)keyboardWasShown:(NSNotification *)notification。在这些方法中,我根据键盘高度滚动内容。在以前的IOS版本中,它可以正常工作,但在IOS8中,它会引起问题。每当我选择任何文本字段时,它都会触发keyboardWasShown,但是无论何时我选择另一个文本字段,即使它已经显示了键盘,它也会再次触发相同的事件。在以前的版本中,它仅触发一次,但对于IOS 8,它仅触发两次。这是我的代码

- (void)registerForKeyboardNotifications {

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (void)deregisterFromKeyboardNotifications {

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidShowNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification {
[LogRecords showLog:@"keyboard shown ... "];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
{
   //scroll content here according to height of keyboard ;
}
}

- (void)keyboardWillBeHidden:(NSNotification *)notification
{
[LogRecords showLog:@"keyboard hide "];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone || (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])))
{
//scroll content here according to height of keyboard ;
}
}

我做错了什么还是新版本有问题?需要帮忙。谢谢。

最佳答案

我一直在iOS8中遇到过同样的事情。

我通过添加用于处理通知的信号灯来解决此问题:

@implementation MyViewController

//workaround to iOS 8 bug where the keyboard notification is fired up twice
static BOOL flag;

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {
    if (flag) {
        return;
    }
    flag = YES;

  //your code for handling keyboard display
}

- (void)ga_keyboardWillHide:(NSNotification *)note {
    if (!flag) {
        return;
    }
    flag = NO;

    // your code for handling keyboard hiding
}
@end

关于ios - iOS8键盘事件无法正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26417759/

相关文章:

iphone - 滚动 UITableView 以使标题不可见

ios - 更改 iOS 键盘动画时间

ios - 雅虎身份验证和获取配置文件详细信息 iOS

ios - sublayerTransform 的动画持续时间被忽略

jquery - anchor 平滑滚动在 WordPress 网站上不起作用

iOS 键盘动画冲突

delphi - 如何使用 delphi 7 将 key 发送到另一个应用程序?

ios - 如何在uibutton之类的消息应用程序上使用角标(Badge)?

IOS/swift : Pass Object in tableview to detail view controller

html - 如何使用鼠标滚轮跳转到 html 中的下一个或上一个 # 部分