我有一个包含文本字段的视图控制器(UICollectionView)。在这个控制器中,我监听UIKeyboardDidShowNotification:s。像这样:
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
并在viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self registerForKeyboardNotifications];
[...]
在另一个视图控制器中,我有一个UITextView. 此控制器还侦听UIKeyboardDidShowNotification. 在我推送第二个视图控制器之前,我将第一个视图控制器作为观察者移除:
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
但是当键盘出现时VC2,keyboardDidShow动作也会被调用VC1,导致不需要的动画。是否有可能以某种方式避免这种行为?
更新
奇怪的是,VC1当我点击返回按钮时,通知操作会被调用VC2。