屏幕旋转时多个NSNotification

时间:2013-10-24 20:34:09

标签: ios objective-c

当我旋转设备时,我收到多个UIKeyboardDidShowNotification和UIKeyboardDidHideNotification通知,我无法弄清楚原因。

我有一个应用程序,可让您编辑每张图片的文本。 (它位于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];
}

当用户点击图片时,View Controller会在View中调用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
}

1 个答案:

答案 0 :(得分:1)

您可以手动关闭键盘并从此方法中清理数据库:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

然后如果您希望键盘在旋转完成后重新启动,请手动调用您的方法从内部显示:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

这可能适合您的需求,也可能不适合您的需求,但这是一种非常常见且干净的技术,可以在Apple处理轮换时“摆脱困境”,然后一旦所有内容重新恢复稳定,就会重新开始工作。< / p>