gpt4 book ai didi

ios - 使用 addObserverForName :usingBlock 时移除 Observer

转载 作者:IT王子 更新时间:2023-10-29 08:19:13 26 4
gpt4 key购买 nike

我有以下代码在加载 View 时添加观察者。

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserverForName:@"com.app.livedata.jsonupdated"
object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
NSLog(@"JSONUPDATED");
}];
}

这很好。但是,当卸载 View 并且我确认 dealloc 被调用时,通知仍在触发。

好像没有关闭这个观察者的方法?

最佳答案

似乎解决方案是在 View 中跟踪对象,然后您可以在 dealloc 方法中引用它。

 id observer = [[NSNotificationCenter defaultCenter] addObserverForName: /* ... */ ];

然后删除如下:

[[NSNotificationCenter defaultCenter] removeObserver:observer];
observer = nil;

关于ios - 使用 addObserverForName :usingBlock 时移除 Observer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com