I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the “Back” button on the navigation bar, I would like to send that array back to VC One.
What’s the best way to do this?
Thanks!
Why not set up a delegate property on your second view controller that the first can register as. Then when the information is stored to the array, it can also be passed back to it’s delegate?
To implement this
At the top of your second view controllers .h file, you’ll need to declare an
@protocolthat the first view controller can implement. A protocol is simular to an interface in other languages. It’s a way of being sure an object implements certain methods, without needing to know specifically what that object is (view controller 1 in this case).and also declare a property for the delegate that the first view controller can set it’s self as before it presents the second
Then in your first view controller
.hfile, implement the protocol as soin the
.hfileand in the
.m, implement the methods declared in the protocolIn order to set the first view controller as the delegate, you need to intercept the segue before it happens, by using a UIStoryBoardDelegate method. Add this to the first view controller
Now you have a pointer to the first view controller from the second, and can call methods and pass back the data, by calling the following method in the second view controller
[self.delegate recieveData:theArrayData];You could also add another method to the protocol to notify the delegate that the second view controller is being dismissed if you wanted. Or use some of the suggestions from the other answers