NSNotificationCenter 添加?

发布于 2024-11-01 15:34:05 字数 901 浏览 0 评论 0原文

我有两个视图,当您从一个视图切换到另一个视图时,它们会向即将加载的视图调用通知以刷新内容。奇怪的是,第一次加载视图时,它会调用一次,下次调用两次,依此类推。我的结论是,这是因为每次加载视图时它们都会不断添加。由于 dealloc 永远不会被调用,所以它仍然存在,并且现在将继续添加。

那么有没有办法在添加通知之前检查通知是否存在以解决此问题?

这是我的 viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadGridNotification:) name:@"ReloadOHGridView" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadBadgeNotification:) name:@"reloadBadge" object:nil];

和我的 dealloc 中的内容:

[[NSNotificationCenter defaultCenter] removeObserver:self];

谢谢!
Coulton

编辑 1:

我在 UINavigationController 中显示我的视图并在它们之间切换。这是我刷新不同视图的代码:

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBadge" object:self];
}

I have two view that when you switch from one to another, they call a notification to the view that's about to get loaded to refresh the content. The weird thing is that the first time the view loads, it will call it once, the next time twice, and so on. I concluded that it's because they keep getting added every time the view loads. Since the dealloc never get's called it's still there and it will keep adding now.

So is there a way to check if the notification exists before getting added to fix this issue?

Here's what I have in my viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadGridNotification:) name:@"ReloadOHGridView" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadBadgeNotification:) name:@"reloadBadge" object:nil];

And my dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Thanks!
Coulton

EDIT 1:

I show my views in a UINavigationController and switch between them. Here's my code to refresh the different view:

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadBadge" object:self];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2024-11-08 15:34:05

每次调用addObserver时,通知中心都会在其内部结构中添加一个条目。这意味着每次您调用 viewDidLoad 时,通知中心都会再次调用您的观察者。

如果您的视图因任何原因被卸载然后重新加载,则 viewDidLoad 将再次被调用。在对象被销毁之前,您的removeobserver不会被调用,这可以解释为什么您的removeobserver不起作用。

您应该检查是否已经使用标志调用了 addObserver,或者在 viewDidUnload 方法中卸载视图时使用 removeObserver 手动删除观察者。

Edit1:或者,您可以在其他地方添加观察者,例如在应用程序委托中吗?

Every time you call addObserver, the notification center will add a entry to its internal structures. This means the notification center will call your observer once more every time you call viewDidLoad.

If your view is unloaded for any reason and then reloaded, then viewDidLoad will get called again. Your removeobserver will not get called until the object is destroyed, which may explain why your removeobserver did not work.

You should either check whether you have already called addObserver with a flag, or manually remove the observer with removeObserver when you unload your view in the viewDidUnload method.

Edit1: Alternatively, can you add the observers somewhere else, like in the App Delegate?

海之角 2024-11-08 15:34:05

当内存紧张时,操作系统将转储您的视图,但会首先调用viewDidUnload;当它必须重新加载它们时,它会调用viewDidLoad。然而,dealloc 仅在对视图的所有引用都被释放时才会被调用,这可能直到您的应用退出后才会发生。

因此,正如 @futureelite7 所指出的,每次重新加载视图时,您都会添加一个新的观察者,但实际上永远不会删除它。

您需要做的就是确保在 viewDidLoad 中添加观察者并在 viewDidUnload 中删除观察者,这样您就不会遇到多重通知问题。不需要旗帜或将观察者放在其他地方。

从您的评论来看,听起来您可能已经尝试过,但我建议仔细检查您的代码,并绝对确定您只在 DidLoad 中添加它们,并且始终在 DidUnload 中删除它们。它就像一个又一个应用程序中的魅力。


编辑为添加

因为您的视图不断被卸载,只有在您手动执行此操作、所有对它的引用都丢失或内存紧张时才会发生这种情况,我建议查看所有三个以帮助确保您正在尽你所能来保持你的观点。

When memory gets tight the OS will dump your view, but will first call viewDidUnload; when it has to reload them it calls viewDidLoad. dealloc, however, is only called when all references to your view have been released, which likely doesn't happen until your app quits.

As a result, as @futureelite7 noted, you're adding a new observer every time your view is reloaded but, effectively, are never removing it.

All you need to do is ensure that the observer is added in viewDidLoad and removed in viewDidUnload you won't have the multiple notification problem. No need for a flag or for putting the observer anywhere else.

From your comments it sounds like you might have tried it, but I suggest going through your code and making absolutely certain you're only adding them in DidLoad and are always removing them in DidUnload. It works like a charm in app after app.


Edited to Add

Because your view keeps getting unloaded, which only happens if you do it manually, if all references to it are lost, or if memory gets tight, I suggest looking at all three to help ensure you're doing what you can to keep your view around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文