iphone - dealloc 中是否需要removeObserver?

标签 iphone memory-management nsnotificationcenter

在我的一个 View Controller 中,它将自身添加为 UITextViewTextDidEndEditingNotification 通知的观察者,如下所示

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(done:) name:UITextViewTextDidEndEditingNotification object:nil];

现在我想知道 - 当 View Controller 被释放时我需要执行以下操作

[[NSNotificationCenter defaultCenter] removeObserver:self];

最佳答案

是的,当观察者被释放时,你应该总是删除它们。否则通知中心将保留对周围已释放对象的引用,并继续尝试将通知转发给它们。

关于iphone - dealloc 中是否需要removeObserver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8665575/

相关文章:

linux - 内存使用分析

cocoa - 我需要保留 NSAlert AlertDidEnd 的 contextInfo 吗?

objective-c - 使用 AVPlayer 循环一段视频

objective-c - -[NSNotificationCenter postNotificationName :object:] Can I pass nil for object?

ios - Uitableview 与 UICollectionviewcell 和 UITableviewcell

javascript - 使用 iPhone 时网站出现水平滚动

iphone - 将 UIButton 添加到 UItableview 单元格

c++ - C/C++ 转换为程序集,低级内存行为 : how is it done?

ios - Swift - 通知观察者被多次调用

ios - 无法 chmod user/Library/Developer/CoreSimulator/Devices NO Such File Or directory