1

What is the best way to send data back to the root view controller before calling the popToRootViewControllerAnimated method? I am trying to signal to the rootviewcontroller that the current viewcontroller no longer exists and keep a record of what viewcontroller has been pushed thru segue.

David
  • 583
  • 1
  • 10
  • 27

3 Answers3

7

You mustn't wait until the current view controller "no longer exists". While the current view controller does exist, the root view controller is the current view controller's navigationController.viewControllers[0]. So you have the reference you need to send a message from this one to that one. Just cast to the root view controller's class and now you can call any public method in the root view controller, e.g.

MyRootViewController* rvc = (MyRootViewController*)(self.navigationController.viewControllers[0]);
rvc.coolData = myCoolData;
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • By the way, the general problem of getting a reference from one object to another in iOS programming is discussed in my book, here: http://www.apeth.com/iOSBook/ch13.html#_instance_visibility – matt Mar 06 '13 at 06:03
  • Sounds good; thanks. Would you say this method is deemed appropriate in terms of Objective-C standards and the whole idea of OO? – David Mar 06 '13 at 19:28
2

There are two basic ways to do this: 1) with an NSNotification message or 2) with a delegate call. There are plenty of tutorials on how to code both. I wrote a small delegate tutorial as an answer to this question: Back button in iphone app and you can find a great NSNotification tutorial here: Send and receive messages through NSNotificationCenter in Objective-C?

Community
  • 1
  • 1
JiuJitsuCoder
  • 1,866
  • 14
  • 13
1

You can subclass UINavigationController and override the necessary methods to track your array. I would recommend keeping a uniqued set of titles for each view controller popped off the stack. As for signaling to the current view controller that the previous view controller doesn't exist, that's what -viewWillAppear is for. If you need finer-grain control, create a protocol for your View Controller instances to conform to that the navigation controller calls out to when it pops one or more of them off the stack.

CodaFi
  • 43,043
  • 8
  • 107
  • 153