【发布时间】:2013-03-30 18:15:17
【问题描述】:
我有一个简单的 viewController,我想监听 UIKeyboardWillHideNotification。因此我有以下代码:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden)
name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillBeHidden
{
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
我正在尝试决定何时将 viewController 作为通知中心观察者移除。当视图控制器在屏幕上时,我只需要知道UIKeyboardWillHideNotification,因此我正在考虑添加以下内容:
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
这就足够了吗?是否有可能在 viewController 仍在屏幕上时调用 viewDidUnload 或 dealloc? 请注意,我正在使用非常基本的 UINavigationController 来处理我的应用程序流。
【问题讨论】:
-
为什么不删除
dealloc中的Observer? -
我可以,但这似乎有点过分,因为当视图控制器不在屏幕上时我不需要知道通知。
标签: iphone ios objective-c nsnotificationcenter viewwillappear