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?
If you want to send data from one to another viewController, here's a way to it:
Say we have viewControllers: viewControllerA and viewControllerB
Now in viewControllerB.h
In viewControllerB.m
In viewControllerA.m
So this is how you can pass data from viewControllerA to viewControllerB without setting any delegate. ;)
This question seems to be very popular here on stackoverflow so I thought I would try and give a better answer to help out people starting in the world of iOS like me.
I hope this answer is clear enough for people to understand and that I have not missed anything.
Passing Data Forward
Passing data forward to a view controller from another view controller. You would use this method if you wanted to pass an object/value from one view controller to another view controller that you may be pushing on to a navigation stack.
For this example, we will have
ViewControllerA
andViewControllerB
To pass a
BOOL
value fromViewControllerA
toViewControllerB
we would do the following.in
ViewControllerB.h
create a property for theBOOL
in
ViewControllerA
you need to tell it aboutViewControllerB
so use anThen where you want to load the view eg.
didSelectRowAtIndex
or someIBAction
you need to set the property inViewControllerB
before you push it onto nav stack.This will set
isSomethingEnabled
inViewControllerB
toBOOL
valueYES
.Passing Data Forward using Segues
If you are using Storyboards you are most likely using segues and will need this procedure to pass data forward. This is similar to the above but instead of passing the data before you push the view controller, you use a method called
So to pass a
BOOL
fromViewControllerA
toViewControllerB
we would do the following:in
ViewControllerB.h
create a property for theBOOL
in
ViewControllerA
you need to tell it aboutViewControllerB
so use anCreate a the segue from
ViewControllerA
toViewControllerB
on the storyboard and give it an identifier, in this example we'll call it"showDetailSegue"
Next, we need to add the method to
ViewControllerA
that is called when any segue is performed, because of this we need to detect which segue was called and then do something. In our example we will check for"showDetailSegue"
and if that's performed we will pass ourBOOL
value toViewControllerB
If you have your views embedded in a navigation controller you need to change the method above slightly to the following
This will set
isSomethingEnabled
inViewControllerB
toBOOL
valueYES
.Passing Data Back
To pass data back from
ViewControllerB
toViewControllerA
you need to use Protocols and Delegates or Blocks, the latter can be used as a loosely coupled mechanism for callbacks.To do this we will make
ViewControllerA
a delegate ofViewControllerB
. This allowsViewControllerB
to send a message back toViewControllerA
enabling us to send data back.For
ViewControllerA
to be a delegate ofViewControllerB
it must conform toViewControllerB
's protocol which we have to specify. This tellsViewControllerA
which methods it must implement.In
ViewControllerB.h
, below the#import
, but above@interface
you specify the protocol.next still in the
ViewControllerB.h
you need to setup adelegate
property and synthesize inViewControllerB.m
In
ViewControllerB
we call a message on thedelegate
when we pop the view controller.That's it for
ViewControllerB
. Now inViewControllerA.h
, tellViewControllerA
to importViewControllerB
and conform to its protocol.In
ViewControllerA.m
implement the following method from our protocolBefore pushing
viewControllerB
to navigation stack we need to tellViewControllerB
thatViewControllerA
is its delegate, otherwise we will get an error.References
NSNotification center It's another way to pass data.
Passing Data back from one class to another (A class can be any controller, Network/session manager, UIView subclass or any other class)
Blocks are anonymous functions.
This example passes data from Controller B to Controller A
define a block
add block handler (listener) where you need a value (for example you need your API response in ControllerA or you need ContorllerB data on A)
Go to Controller B
fire block
Another Working Example for Blocks
Passing Data between FirstViewController to SecondViewController as below
For example:
FirstViewController String value as
so we can pass this value in second class using below step
1>We need to crate string object in SecondViewController.h file
2>Need to declare property as below below declaration in .h file
3>Need synthesize that value in FirstViewController.m file below header declaration
and in FirstViewController.h :
4>In FirstViewController,From which method we navigate to second view please write below code in that method.
If you want to pass data from one controller to other try this code
FirstViewController.h
SecondViewController.h
FirstViewController.m
I know this is a beaten subject but for those looking to answer this question with a SWIFT slant and want a bare-bones example, here my go-to method for passing data if you are using a segue to get around.
It is similar to the above but without the buttons, labels and such. Just simply passing data from one view to the next.
Setup The Storyboard
There are three parts.
This is a very simple view layout with a segue between them.
Here is the setup for the sender
Here is the setup for the receiver.
Lastly, the setup for the segue.
The View Controllers
We are keeping this simple so no buttons, not actions, we are simply moving data from the sender to the receiver when the application loads and then outputting the transmitted value to the console.
This page takes the initially loaded value and passes it along.
This page just sends the value of the variable to the console when it loads. By this point, our favorite movie should be in that variable.
That is how you can tackle it if you want to use a segue and you don't have your pages under a navigation controller.
Once it is run it should switch to the receiver view automatically and pass the value from the sender to the receiver, displaying the value in the console.
There are tons of ways to do this and it's important to pick the right one. Probably one of the biggest architectural decisions lies on how the model code will be shared or accessed throughout the app.
I wrote a blog post about this a while back: Sharing Model Code. Here's a brief summary:
Shared data
One approach is to share pointers to the model objects between view controllers.
Since prepare for segue is the most common here is an example:
Independent access
Another approach is to handle a screen full of data at a time and instead of coupling the view controllers to each other couple each view controller to single data source that they can get to independently.
The most common way I've seen this done is a singleton instance. So if your singleton object was
DataAccess
you could do the following in the viewDidLoad method of UIViewController:There are addition tools that also help pass along data:
Core Data
The nice thing about Core Data is that it has inverse relationships. So if you want to just give a NotesViewController the notes object you can because it'll have an inverse relationship to something else like the notebook. If you need data on the notebook in the NotesViewController you can walk back up the object graph by doing the following:
Read more about this in my blog post: Sharing Model Code