观察者多次呼叫

时间:2015-08-12 16:24:28

标签: objective-c ios8 ios9 observers

我在viewdidload中添加观察者 -

   [[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aMethod) name:aName object:nil];

但每当我打开视图时,它会添加观察者。如何管理?

1 个答案:

答案 0 :(得分:0)

  - (void)viewWillDisappear:(BOOL)animated {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];

   }

  - (void)viewWillAppear:(BOOL)animated {

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aMethod) name:aName object:nil];
}