gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:38:15 25 4
gpt4 key购买 nike

嗨,我正在开发一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com