2011-12-29 94 views
3

我認爲控制器之一,它增加了本身的UITextViewTextDidEndEditingNotification通知觀察者,像下面這樣做是dealloc上必需的removeObserver?

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

現在我想知道 - 我需要做以下時,視圖控制器dealloc'd

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
+0

Docs:「如果您的應用程序面向iOS 9.0或更高版本或MacOS 10.11及更高版本,則無需在其解除分配方法中取消註冊觀察者,如果您的應用程序面向早期版本,請務必調用removeObserver:name:object :在觀察者或addObserver中指定的任何對象之前:選擇器:name:object:被釋放。「 – Liviu 2017-07-31 16:15:56

回答

4

是的,你應該總是刪除任何觀察者,當他們被dealloc'd。否則通知中心將繼續引用現在已解除分配的對象並繼續嘗試將通知轉發給它們。

相關問題