In my application I have three view controllers of concern. The first contains a map and a button to open the second view controller. The second view controller contains a searchable table and then when the user selects a row it loads the relevant data in the third view controller. This all works well!
Now the intention is that when the user presses the Show on Map button in the third view controller it’ll pass back the data (in this case two double values for the coordinates) to the first view controller so that the first view controller can then focus in on these coordinates.
I’ve followed Apple’s documentation (the BirdSighting tutorial) as well as previous questions/answers on SO but I’ve noticed one issue.
I can’t really find a place to set the thirdviewcontroller’s delegate to be the first view controller. Typically I’d enter the following code in the first VC but I’m not creating an instance of the third VC – that happens in the second one:
thirdVC.delegate = self; //set self as the delegate
So what should I do?
Thanks
Delegate is one of many mechanisms to accomplish what you need. The suggestion by @onnoweb is perfectly suitable, though this can get messy passing around delegate pointers.
KVO:
You can also consider KVO, putting your data into a model object, having VC3 update the model object, and VC1 an observer of those values.
NSNotificationCenter:
Another alternative is NSNotificationCenter
in VC3, use this to send a broadcast (set your dictionary to contain your latitude/longitude coordinates):
in VC1:
register to receive the broadcasts:
and handle the broadcasts:
and unregister in your dealloc