Passing Data(String) from Child View Controller to Parent VC using easiest Way .
i tried a Couple of ways , but got lost , can some one tell me the best way .
Passing Data(String) from Child View Controller to Parent VC using easiest Way . i
Share
Srikanth is correct. If you have a segue from a view controller (our “first” view controller) to another (our “second” view controller), all you need to do is to create a property in the second one that points to the first one. You will just have the first view controller make sure to set that pointer before it performs the transition. Having done that, the second controller can update properties or invoke methods of the first controller.
For details, see Configuring the Destination Controller When a Segue is Triggered in the View Controller Programming Guide for information on how to set a property in the second view controller (in this case, that property will be a weak pointer to the first controller) in
prepareForSegue. Then, as Srikanth says, the second controller can use that pointer to update properties in the first one.So, in your second view controller, have a property (note carefully, it should be
weak) that points back to the first view controller:In SecondViewController.h:
Then in the FirstViewController.m:
So, if your first view controller had, for example, a property of
favoriteColor:Then, the second view controller could use its
firstViewControllerproperty to update thisfavoriteColor, like so:Clearly:
Replace
FirstViewControllerandSecondViewControllerwith the appropriate class names;Make sure that your second view controller’s .m file does an
#importof the first view controller’s .h; andMake sure you’ve specified a segue identifier in Interface Builder for your segue from the first controller to the second one and adjust the
prepareForSegueabove, replacingYourSegueIdentifierHerewith your identifier.In iOS 6, you can also accomplish this via an unwind segue. You’d just have the
prepareForSegueof the second view controller update the property of the unwind segue’s destination controller (i.e. the first controller). What’s nice is that unwind segues can go back an arbitrary number of levels, so for more complicated scenarios, it’s very nice. It is iOS 6, or higher, only, though.To do unwind segue’s, first you must define an unwind action in the first view controller (identified as such by the combination of the
IBActionreturn type and theUIStoryboardSegueparameter), e.g.:Then, the second (or third or …) view controller can create an unwind segue by control-dragging in Interface Builder from a button to the exit icon in the scene’s dock. You can have the controller from which you’re unwinding do the logical
prepareForSegueto pass information back to the first view controller.By the way, you used the terms “parent” and “child”, but I wanted to make it clear that I assumed that you were not talking about the more advanced topic of view controller containment, in which a view controller is invoking other view controllers to facilitate the presentation of a single screen of information (as opposed to transitioning between different scenes in an app). As rdelmar notes in our comments below, the terms “parent” and “child” controllers, strictly speaking, more properly imply that one is using view controller containment.
Obviously, if you are using view controller containment, then clearly the discussion of segues,
prepareForSegue, etc., don’t apply. Furthermore, a properly implemented container view controller guarantees that the child controllers can actually use theUIViewControllerproperty ofparentViewController, without needing to define our own property to reference the parent. All you need to do is to cast/defineparentViewControllerto be the right subclass, and then you can access your subclassed properties very easily.