Search code examples
iosviewdidloadnsnotificationcenter

register for notifications in viewDidLoad


I am registering for notifications in viewDidLoad. Every time a memory warning occurs, is registering multiple times or the iOS just ignores it if is already registered? In case of multiple registers, what is the best practice to avoid that? I thought in a boolean to handle if is the first time viewDidLoad executes but I'm not sure if is a good practice.

Thanks in advance!


Solution

  • Just unregister them in viewDidUnload - since you probably do some UI operations in response to the notifications, not much you can do with the unloaded view anyway.

    If the notifications are for something other than UI, then you probably should register them in the constructor instead, or, better yet, move them into a separate non-UI class.