removeObserver()是否删除所有观察者?

4th*_*ace 16 nsnotificationcenter swift

以下是否删除了所有NSNotificationCenter.defaultCenter,其中添加了名称视图?

NSNotificationCenter.defaultCenter().removeObserver(self)
Run Code Online (Sandbox Code Playgroud)

如果我在viewDidLoad()的相同视图中有以下内容,是否将使用上面的单行删除它们?

NSNotificationCenter.defaultCenter().addObserver(self, selector: "method1", name: UITextFieldTextDidChangeNotification, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "method2", name: UITextViewTextDidChangeNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

Dun*_*n C 36

是的,无论您指定的通知名称,对象或选择器如何,该removeObserver(self)调用都将删除您使用addObserver:selector:name:object:self的观察者添加的所有观察者.

removeObserver(self)在对象的deinit方法中的任何地方使用该方法是一个坏主意,因为某些系统类(或您定义的对象的子类)可能添加了您不知道的观察者.该方法调用是一个"焦土"调用,从对象中删除所有观察者.

相反,您应该removeObserver:name:object:只调用并删除您添加的观察者.


小智 2

从接收者\xe2\x80\x99s 调度表中删除指定给定观察者的所有条目。\n https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/index。 html#//apple_ref/occ/instm/NSNotificationCenter/removeObserver

\n\n

所以我想只有当所有观察者与参数指定的相同时,它才会删除所有观察者。

\n