我越来越多UIKeyboardDidShowNotification和UIKeyboardDidHideNotification通知时我旋转设备,我想不通为什么多NSNotifications。当屏幕旋转
我有一个应用程序,可以让你编辑每幅画面的文字。 (它在UITextView中。)大多数情况下,我需要向上滑动文本以便在键盘上方看到它,然后在完成编辑时将其滑下。然后我进行数据库更新以保存新文本。我使用通知来告诉我键盘何时显示以及何时消失。当用户点击键盘上的键盘关闭图标时,它可以在iPad上正常工作。如果用户滑动到下一页并且iOS关闭键盘,它也可以正常工作。由于iPhone和iPod没有键盘关闭键,所以我编写了一个方法在点击图片或背景时关闭键盘。它在那里也能正常工作。但是,当我旋转设备时,我收到多个隐藏和显示通知。我不知道为什么。
非但没有一个UIKeyboardDidHideNotification通知,我得到一个躲,一个节目,一躲,然后表演。
2:39:44.200 Picts for SLPs[16533:907] keyboardDidHide called. Keyboard showing flag is YES.
2:39:51.751 Picts for SLPs[16533:907] keyboardDidShow called. Keyboard showing flag is NO.
2:39:55.224 Picts for SLPs[16533:907] keyboardDidHide called. Keyboard showing flag is YES.
2:39:56.124 Picts for SLPs[16533:907] keyboardDidShow called. Keyboard showing flag is NO.
我发布了下面的相关代码。它主要来自StackOverflow帖子(谢谢你们)。
在我的课,显示我开始通知初始化时的图片。
- (id)initWithParentView:(UIView *)parentview {
self = [super init];
if (self) {
_parentView = parentview;
if (ALLOW_DATABASE_EDITING) [self startNotifications];
}
return self;
}
- (void)startNotifications {
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
视图控制器当用户轻敲该图片调用在查看hideKeyboard方法。
- (void)dismissKeyboard {
if (self.showArtic.keyBoardIsShowing) {
[self.showArtic hideTheKeyboard];
}
}
resignFirstResponder发送一个关闭键盘
- (void)hideTheKeyboard {
id <ShowArticDelegate> SA_delegate = _delegate;
// Don't update the database when there is no text.
if (![self.editableTextView.text isEqualToString:@""]) {
[SA_delegate updateTextInDatabase:self.editableTextView.text];
}
[self.editableTextView resignFirstResponder];
}
这些方法的响应通知的通知。
- (void)keyboardDidHide:(NSNotification *)notification {
NSLog(@"keyboardDidHide called. Keyboard showing flag is %@.", self.keyBoardIsShowing ? @"YES" : @"NO");
self.keyBoardIsShowing = NO;
// Move the text, update the database
}
- (void)keyboardDidShow:(NSNotification *)notification {
NSLog(@"keyboardDidShow called. Keyboard showing flag is %@.", self.keyBoardIsShowing ? @"YES" : @"NO");
self.keyBoardIsShowing = YES;
// Move the text
}
我主要是好奇,是什么原因导致的通知火灾。您的解决方案适用于此特定情况 - 尽管我没有使用它。多个通知发生的唯一不好的事情是数据库更新会被多次调用。由于屏幕旋转时文本字段中没有文本,因此数据库中会出现空白行。如果我查看文本是否空白,那么我很好。 – JScarry
别人或许可以用,为什么有些旋转事件似乎产生更多的电话比他们需要一个更细致的解释这里附和,但我从来没有想通了,究竟为什么无论是和人一直把设备的内部工作旋转作为一个“黑匣子”或私人API供Apple整理。这可能与iOS如何使用加速度计确定旋转发生有关(类似于姿势识别实际上是一个漫长而复杂的一系列步骤),但我从来没有足够的测试它来辨别确切的韵律或原因。 –
我怀疑这就是为什么他们给了我们两个很好的机会,之前和之后轮换清理和避开。但这只是我的理论。 –