0

Scenario:

  • Storyboard with UINavigationController as initial controller.
  • MainVC (navigation controller's root vc) needs the user to choose a location on a map.
  • MainVC pushes a VC containing a MapKit map and adds itself as listener for a notification the MapVC sends out when the user chooses a location.
  • MapKit is a memory hog, we all know that. iOS gives me a memory warning, I do all the things that need to be done, then iOS deallocates all it can deallocate, including the MainVC.
  • MapVC sends out the notification but there's nobody to listen to it. The location the user chose is lost, like tears in rain.

Given this, what's a reliable way to pass that location data when going back to MainVC? I even thought of writing it down to ~/tmp (which is something I use to do when dealing with large amount of data like images) but that seems like a waste of machinery. Isn't there a mechanism I can hook to, like an event fired when the navigation controller goes back to the previous VC? Like, having access to something like the prepareForSegue: but on the opposite direction would be nice.

EDIT I tried going for delegation but it seems my delegate gets released nonetheless. Am I stuck with having to write to ~/tmp?

Morpheu5
  • 2,610
  • 6
  • 39
  • 72
  • Delegation is the way to go - it sounds like you've got an issue with your delegate object being deallocated? – petert Feb 20 '12 at 12:23
  • I can only add that if I comment out the [super didReceiveMemoryWarning] in the MainVC (which is now the MapVC.delegate) it doesn't get deallocated. I think this qualifies as bad behaviour though. – Morpheu5 Feb 20 '12 at 14:31

1 Answers1

0

See my answer here for a detailed explanation of setting up a delegate and delegate protocol - here

Community
  • 1
  • 1
T.J.
  • 3,942
  • 2
  • 32
  • 40
  • Ok, the delegate works and is kept up even after a memory warning. The mistery is now: why do the fields and buttons I have in it don't keep their values and states? Specifically, I have a UITextView with some text that gets cleared, and some buttons that should be highlighted/enabled but they're not. – Morpheu5 Feb 20 '12 at 16:12
  • Also I can confirm those fields keep their correct values and enabled/highlighted statuses, they're just not shown when the MapVC is popped. – Morpheu5 Feb 20 '12 at 16:16
  • I don't understand the new issue. I have a suggestion. Accept this answer, convert to ARC to clear up your memory problems, and write up a new question with your problems that remain. To convert your project to ARC in Xcode select "edit" then "refactor" and finally "Convert to ARC". Take a snapshot of your project first with "file" "create snapshot" in case the conversion has problems. – T.J. Feb 20 '12 at 17:50
  • http://stackoverflow.com/questions/9366316/controls-in-a-viewcontroller-losing-their-state-after-memory-warning-while-off-s Here's the question, thanks for your help. – Morpheu5 Feb 20 '12 at 18:20