Reputation: 1108
I have a basic question regarding removing observer.
I have a ViewController parent class which is inherited by 3 ViewController child classes. eg. BookVC -> BookHotelVC, BookFlightVC, BookTrainVC
Here, I added an observer in the viewDidLoad
of parent class (I do [super viewDidLoad]
in child ViewControllers) which notifies a method written in parent class. My code-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BookingCompleted:) name:@"BookingCompleted" object:nil];
Now I want to remove the observer when I move away from any of the child ViewControllers, but I can't write [super dealloc]
in dealloc
of each child ViewController because ARC doesn't permit this.
How can I remove the observer which is set ? Because whenever I move to child ViewController, a new observer is added which causes weird things (like, calling that method twice/thrice... - invoking alert twice/thrice...).
Kindly suggest.
Upvotes: 0
Views: 744
Reputation: 26068
Removing the observers in dealloc
is fine, do not call [super dealloc]
(as you saw, with ARC enabled, the compiler won't let you), simply write:
- (void)dealloc {
[self removeYourObservers];
}
Upvotes: 1
Reputation: 6480
Just don't call super
! In ARC it's not required (see http://clang.llvm.org/docs/AutomaticReferenceCounting.html#dealloc).
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Upvotes: 0