关于ViewWillAppear和ViewWillDisapper的NSNotificationCenter [英] NSNotificationCenter with respect to ViewWillAppear and ViewWillDisapper

查看:146
本文介绍了关于ViewWillAppear和ViewWillDisapper的NSNotificationCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的viewController,我想听 UIKeyboardWillHideNotification 。因此我有以下代码:

I have a simple viewController that I want to listen for UIKeyboardWillHideNotification. Therefore I have the following code:

- (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作为通知中心观察者。当viewcontroller在屏幕上时,我只需要知道 UIKeyboardWillHideNotification ,因此我正在考虑添加以下内容:

I'm trying to decide when to remove the viewController as an notification center observer. I only need to know about the UIKeyboardWillHideNotification when the viewcontroller is on screen, thus I'm thinking about adding the following:

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

这是否足够?是否有可能在viewController仍在屏幕上时调用 viewDidUnload dealloc 请注意,我正在使用非常基本的 UINavigationController 来获取我的应用流程。

Is this sufficient? Is there ever a chance that viewDidUnload or dealloc will get called while the viewController is still on screen? Note that I'm using a very basic UINavigationController for the flow of my app.

推荐答案

viewWillAppear 中注册通知并在 viewWillDisappear 中取消注册它似乎是一个干净,对称的解决方案我。

Registering the notification in viewWillAppear and unregistering it in viewWillDisappear seems to be a clean and symmetric solution to me.

注意 viewWillAppear 可以在 dealloc (例如,如果另一个视图控制器被推入VC,或者如果您在标签栏控制器之间切换。)如果您在 viewWillAppear 中注册通知并仅在 dealloc 然后你会得到重复的注册(比较针对iOS / iPhone用户的警告重复NSNotification观察)并且对于单个通知事件多次调用已注册的选择器。

Note that viewWillAppear can be called multiple times before dealloc (e.g. if another view controller is pushed onto your VC, or if you switch between tab bar controllers.) If you register the notification in viewWillAppear and unregister it only in dealloc then you will get duplicate registrations (compare Warning for iOS/iPhone users about duplicate NSNotification observations) and the registered selector is called multiple times for a single notification event.

我实际上更喜欢基于块的观察者注册方法

I actually prefer the block-based observer registration method

addObserverForName:object:queue:usingBlock:

返回一个不透明对象,用于再次移除观察者。将此返回值存储到视图控制器的实例变量中有助于跟踪观察者是否已注册,因此有助于避免重复注册。

which returns an opaque object which is used for removing the observer again. Storing this return value into an instance variable of your view controller helps to keep track if the observer is already registered or not, and therefore helps to avoid duplicate registrations.

这篇关于关于ViewWillAppear和ViewWillDisapper的NSNotificationCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆