1

In this Navigation Controller App, I have a button in the TAN ViewController that when clicked on posts an NSNotification.
Project Storyboard

-Both the Blue (Root) ViewController and the Green ViewController are listening to this same notification
-The Blue ViewController successfully responds to the notification's arrival
-The Green ViewController DOES NOT respond to it successfully. It doesn't even see it arriving (even though I'm using the exact same code as the Blue ViewController's code to register to listen for this Notification.)
-In both cases I'm registering to listen for the notification in viewDidLoad:

 override func viewDidLoad() {
 super.viewDidLoad()
 // Set Up Listening to Notification:
 let notificationCenter = NSNotificationCenter.defaultCenter()
 notificationCenter.addObserver(self, selector: "handleNotification:", name:"User Specs Notification", object: nil)
 }

I also make sure to visit the GREEN ViewController - which automatically triggers its viewDidLoad and executes the Notification listening code - before I ever go to the TAN ViewController and post the Notification from there. But again, when I go back to the Blue/Root ViewController, I see that it has responded correctly to the Notification's arrival, but when I then go to the Green ViewController - nothing has changed.

Any ideas what's going on?
Should I be registering to listen for the Notification someplace other than viewDidLoad? If so, where? And why is it still working for the Blue/Root ViewController - where I am registering in viewDidLoad - but not in the Green ViewController? Where's the logic in this?

asked Mar 8, 2015 at 19:37

1 Answer 1

1

The green view controller is deallocated when you go back to blue VC. So it's not there to receive the notification.

answered Mar 8, 2015 at 19:44
Sign up to request clarification or add additional context in comments.

2 Comments

I thought it stayed in memory until you actually purge it. So if its deallocated, how can you get it to still be aware of events/Notifications happening other places in the App? Or do you have to use another strategy altogether?
The idea, in case of Storyboards, is that you give the destination VC all the information it needs in the prepareForSeque method. While the VC is visible it can listen to notifications but it should deregister for notifications when it is no longer (viewWillDisappear).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.