SimplyLearning
SimplyLearning

Reputation: 279

NSNotification: possible to receive when Controller doesn't exist yet?

If I'm using NSNotifications to tell my UITableViews (in different Controllers) to add a new Cell, but the Controller in which that UITableView lies has not been "accessed" yet or loaded/init'd, that Controller will not receive the message (as my NSNotificationCenter is setup in each of my viewDidLoad). This is the expected behavior as I understand.

Now my question: Is there a way to have the Controller's that haven't been accessed yet once the app launches receive the notification? Or is there a better approach of posting to another UIViewController's UITableView from a different UIView rather than using NSNotifications?

I saw a similar question asked a few years back, so if anyone can chime in, would be greatly appreciated!

Upvotes: 0

Views: 65

Answers (1)

Lebyrt
Lebyrt

Reputation: 1340

If you want your controller to receive a notification before it's accessed (i.e. its view is loaded and viewDidLoad is called) then you need to subscribe for a notification in one of the init methods:

  1. If you want to create your controller programmatically then you need to do it in init or initWithStyle: methods.

  2. If you want to create your controller programmatically from .xib file then you need to do it in initWithNibName:bundle: method.

  3. If you want to create your controller from .storyboard file then you need to do it in initWithCoder: method.

Upvotes: 1

Related Questions