簡體   English   中英

從另一個視圖控制器中刪除通知觀察者

[英]Remove notification observer from another view controller

嗨,我正在開發Iphone應用程序,其中正在為UIApplicationWillEnterForegroundNotification注冊一個通知觀察器。 現在,我想從另一個視圖控制器中刪除該對象。 我的代碼看起來像

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(applicationBecomeActive)
 name:UIApplicationWillEnterForegroundNotification
 object:nil]; 

我正在創建一種刪除觀察者的方法:

-(void) removeObserver
{
  [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

我從其他視圖控制器調用此方法,但它不起作用。 我想我必須存儲觀察者。 但是我不知道該怎么做。 需要幫忙。 謝謝。

第二視圖控制器需要引用第一視圖控制器的實例。 假設它被保存在一個屬性中:

@property (nonatomic, strong) FirstViewControler *firstViewController;

然后,以觀察者身份刪除第一視圖控制器的代碼如下所示:

- (void)removeObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.firstViewController]; 
}

缺少的部分是:您必須將屬性設置在某個地方。 在哪里做取決於您的代碼。

暫無
暫無

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

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