1

Given this code in the viewDidLoad:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:
 UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:
 UIKeyboardWillHideNotification object:nil];

I'm asking myself if I need to delete the observers when I unload the view (or something similar).

It looks a bit like this question, but that question does not discuss if dealloc is deprecated since the use of ARC (edit: see comments in accepted answer).

But since ios updates all the time and I have no clue if you still would need to call dealloc and I've never seen a piece of code how to do this (delete the observers that is), some help would be appreciated :)

Community
  • 1
  • 1
Melvin Roest
  • 1,392
  • 1
  • 15
  • 31

3 Answers3

3

Yes, you do. Override dealloc in your view controller like so:

- (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Benjamin Mayo
  • 6,649
  • 2
  • 26
  • 25
2

In general, you should remove an observer from a notification center when the observer is no longer needed -- either you don't need it to continue observing, or the object itself is being deallocated. You don't need to remove an object observing UIKeyboardWillHideNotification simply because the keyboard is going away, but you do need to remove it if you're getting rid of the object itself.

Consider what would happen if you didn't remove the observer before it was deallocated: if the notification it was watching for ever occurred, the notification center would try to send a message to that object. That would cause either a crash or some behavior that you don't expect, since the pointer the notification center used to send the message no longer points to the object that's supposed to observe the notification.

Caleb
  • 124,013
  • 19
  • 183
  • 272
1

It is always good to have your notification observer's unregistered while they are not required.

Since your observer deals with Keyboard which is a visual element, have it unregistered, i.e. removed from observer on viewDidDisappear and have it re-register in viewWillAppear (something which i follow.).

This is required coz, in case you are pushing a new view controller over the one you are using, the observer will still be registered and can cause erroneous behaviour, and sometimes, if your view controller get deallocated, the notification will still have the observer registered and can lead to crash. Coz notification centre defaultCenter is a singleton instance for process and will be there for its lifetime.

Post iOS 6.0 you can call it in viewDidUnload. But iOS 6.0 that is deprecated.

egghese
  • 2,193
  • 16
  • 26
  • Yea I noticed, it was one of the more implicit reasons why I asked this question. Because I am using arc and I'd like to generalize to ios 6 as well. – Melvin Roest Jul 20 '13 at 19:52
  • 2
    You can continue to use `-dealloc` under ARC to remove an object from a notification center and do other cleanup when an object is deallocated; you just don't need to call `[super dealloc]` or release objects pointed to by the object's ivars. `-viewDidUnload` isn't a good choice because, as you point out, it's deprecated. – Caleb Jul 20 '13 at 19:56