Frage

I'm trying to subscribe myViewController to a custom NSNotification. The view heirarchy looks like this:


window
   - rootViewController.view
      - scrollView
          - myViewController.view

The notification is being sent to the [NSNotificationCenter defaultCenter] from a button inside the rootViewController's view. The rootViewController listens for the notification and responds to it just fine.

I have the same exact "listening" code inside myViewController, but it's not receiving the notification for some reason. If it's part of the app it should receive the notification, correct? I have a debug message inside myViewController's initWithNibName method, so I know that it's subscribing to the notification. I even tried having myViewController listen for ALL notifications by setting the notification name to nil. For example:


NSLog(@"main view controller initialized");

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarVisibilityChanged:) name:nil object:nil];

But no luck there either. Has anyone seen anything like this happen before with NSNotifications?

Any ideas?

Thanks!

War es hilfreich?

Lösung

I guess the view controller is deallocated. It's a common mistake not to retain a view controller when creating it programmatically.

Andere Tipps

If you are sure that the observation is set up correctly, my only explanation for not receiving any notifications is, that the controller is deallocated before any notifications are being sent.

The NSNotification mechanism is not dependent on neither view hierarchy nor thread. If you have a (living) object that has been set up as an observer it will definitely receive notifications.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top