I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following:
I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with a UITableViewController
and I have enabled multiple selections.
My question is, how do I transfer the data from one view to another? I will be holding the selections on the UITableView
in an array, but how do I then pass that back to the previous data entry form view so it can be saved along with the other data to Core Data on submission of the form?
I have surfed around and seen some people declare an array in the app delegate. I read something about Singletons but don't understand what these are and I read something about creating a data model.
What would be the correct way of performing this and how would I go about it?
After more research it seemed that Protocols and Delegates is the correct/Apple prefered way of doing this.
I ended up using this example
Sharing data between view controllers and other objects @ iPhone Dev SDK
Worked fine and allowed me to pass a string and an array forward and back between my views.
Thanks for all your help
Swift
There are tons and tons of explanations here and around StackOverflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it).
Passing data forward to the next View Controller
The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
Create the storyboard layout in the Interface Builder. To make the segue, you just Control click on the button and drag over to the Second View Controller.
First View Controller
The code for the First View Controller is
Second View Controller
And the code for the Second View Controller is
Don't forget
UITextField
and theUILabel
.Passing data back to the previous View Controller
To pass data back from the second view controller to the first view controller, you use a protocol and a delegate. This video is a very clear walk though of that process:
The following is an example based on the video (with a few modifications).
Create the storyboard layout in the Interface Builder. Again, to make the segue, you just Control drag from the button to the Second View Controller. Set the segue identifier to
showSecondViewController
. Also, don't forget to hook up the outlets and actions using the names in the following code.First View Controller
The code for the First View Controller is
Note the use of our custom
DataEnteredDelegate
protocol.Second View Controller and Protocol
The code for the second view controller is
Note that the
protocol
is outside of the View Controller class.That's it. Running the app now you should be able to send data back from the second view controller to the first.
The OP didn't mention view controllers but so many of the answers do, that I wanted to chime in with what some of the new features of the LLVM allow to make this easier when wanting to pass data from one view controller to another and then getting some results back.
Storyboard segues, ARC and LLVM blocks make this easier than ever for me. Some answers above mentioned storyboards and segues already but still relied on delegation. Defining delegates certainly works but some people may find it easier to pass pointers or code blocks.
With UINavigators and segues, there are easy ways of passing information to the subservient controller and getting the information back. ARC makes passing pointers to things derived from NSObjects simple so if you want the subservient controller to add/change/modify some data for you, pass it a pointer to a mutable instance. Blocks make passing actions easy so if you want the subservient controller to invoke an action on your higher level controller, pass it a block. You define the block to accept any number of arguments that makes sense to you. You can also design the API to use multiple blocks if that suits things better.
Here are two trivial examples of the segue glue. The first is straightforward showing one parameter passed for input, the second for output.
This second example shows passing a callback block for the second argument. I like using blocks because it keeps the relevant details close together in the source - the higher level source.
If you want to send data from one to another viewController, here's a way to it:
Say we have viewControllers: ViewController and NewViewController.
in ViewController.h
in ViewController.m
In NewViewController.h
In NewViewController.m
So this way we can pass the data from one viewcontroller to another view controller...
This is a very old answer and this is anti pattern, please use delegates. Do not use this Approach !!
1. Create the instance of first View Controller in the second View Controller and make its property
@property (nonatomic,assign)
.2. Assign the
SecondviewController
instance of this view controller.2. When you finish the selection operation copy the array to first View Controller,When u unload the SecondView ,FirstView will hold the Array Data.
Hope This Helps.
Create the property on next
view controller .h
and define getter and setter.Add this
property
in NextVC.h on nextVCAdd
@synthesize indexNumber;
in NextVC.mAnd last