簡體   English   中英

如何刪除觀察者

[英]How to remove observer

我有一個啟用了ARC的項目

viewDidLoad中添加的觀察者很少

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSipNotification:) name:@"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncExtensionData:) name:@"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLocalNotification:) name:@"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outgoingCall:)  name:@"outgoingCall" object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playRingtone)  name:@"playRingtone" object:nil];

我想刪除所有觀察者,所以我在viewDidUnload中添加了以下行

[[NSNotificationCenter defaultCenter] removeObserver:self];

現在我的問題是,這會刪除所有觀察者嗎?

如果沒有,怎么辦?

UPDATE

如果我想刪除一個觀察者,該怎么辦?

你能幫我嗎。

是的,它將刪除所有觀察者。

 [[NSNotificationCenter defaultCenter] removeObserver:self];

您可以像這樣刪除特定的觀察者...

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"syncExtensionData" object:nil];

在我的應用程序中,我使用了以下通知:

對於特定的觀察者,請刪除此方式:

 -(void)viewWillAppear:(BOOL)animated
 {

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
    //right whetever you want
}
 - (void)viewWillDisappear:(BOOL)animated
{

  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

可能會對您有幫助。

是的,它將刪除您班上的所有觀察員。

您可以使用以下命令刪除單個觀察者:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"getSipNotification" object:nil];

刪除單個觀察者。

在iOS6和更高版本中不建議使用viewDidUnload,因此您的觀察者永遠不會從iOS6和更高版本的通知中心中刪除。 要刪除單個觀察者,請嘗試

  -(void)removeObserver:(id)notification觀察者名稱:(NSString *)notificationName對象:(id)notificationSender 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM