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?
Passing data back from ViewController 2(destination) to viewController 1(Source) is the more interesting thing. Assuming you use storyBoard those are all the ways i found out:
Those were discussed here already.
I found there are more ways:
-Using Block callbacks:
use it in the
prepareForSegue
method in the VC1-Using storyboards Unwind (Exit)
Implement a method with a UIStoryboardSegue argument in VC 1, like this one:
In the storyBoard hook the "return" button to the green Exit button(Unwind) of the vc. Now you have a segue that "goes back" so u can use the destinationViewController property in the prepareForSegue of VC2 and change any property of VC1 before it goes back.
Another option of using storyboards Undwind (Exit) - you can use the method you wrote in VC1
And in the prepareForSegue of VC1 you can change any property you want to share.
In both unwind options you can set the tag property of the button and check it in the prepareForSegue.
Hope i added something to the discussion.
:) Cheers.
There are multiple methods for sharing data.
You can always share data using
NSUserDefaults
. Set the value you want to share with respect to a key of your choice and get the value fromNSUserDefault
associated to that key in the next view controller.You can just create a property in
viewcontrollerA
. Create an object ofviewcontrollerA
inviewcontrollerB
and assign the desired value to that property.You can also create custom delegates for this.
I was searching this solution for long time, Atlast I found it. First of all declare all the objects in your SecondViewController.h file like
Now in your implementation file allocate the memory for those objects like this
Now you have allocated the memory for
Array
and object. now you can fill that memory before pushing thisViewController
Go to your SecondViewController.h and write two methods
in implementation file you can implement the function
expecting that your
CustomObject
must have a setter function with it.now your basic work is done. go to the place where you want to push the
SecondViewController
and do the following stuffTake care for spelling mistakes.
There are multiple options for Passing Data between View Controllers.
I am going to rewrite his logic in Swift with latest iOS Framework
Step 1. Declare variable in ViewControllerB
Step 2. Print Variable in ViewControllerB' ViewDidLoad method
Step 3. In ViewControllerA Pass Data while pushing through Navigation Controller
So Here is the complete code for :
ViewControllerA
ViewControllerB
Step 1. Create Segue from ViewControllerA to ViewControllerB and give Identifier = showDetailSegue in Storyboard as shown below
Step 2. In ViewControllerB Declare a viable named isSomethingEnabled and print its value.
Step 3. In ViewControllerA pass isSomethingEnabled's value while passing Segue
So Here is the complete code for :
ViewControllerA
ViewControllerB
Step 1. Declare Protocol ViewControllerBDelegate in ViewControllerB file but outside the class
Step 2. Declare Delegate variable instance in ViewControllerB
Step 3. Send data for delegate inside viewDidLoad method of ViewControllerB
Step 4. Confirm ViewControllerBDelegate in ViewControllerA
Step 5. Confirm that you will implement delegate in ViewControllerA
Step 6. Implement delegate method for receiving data in ViewControllerA
So Here is the complete code for :
ViewControllerA
ViewControllerB
Step 1. Set and Post data in Notification observer in ViewControllerB
Step 2. Add Notification Observer in ViewControllerA
Step 3. Receive Notification data value in ViewControllerA
So Here is the complete code for :
ViewControllerA
ViewControllerB
Step 1. Declare block in ViewControllerB
var authorizationCompletionBlock:((Bool)->())? = {_ in}
Step 2. Set data in block in ViewControllerB
Step 3. Receive block data in ViewControllerA
So Here is the complete code for :
ViewControllerA
ViewControllerB
You can find complete sample Application at my GitHub Please let me know if you have any question(s) on this.
NewsViewController
NewsDetailViewController.h
NewsDetailViewController.m
There are various ways by which a data can be received to a different class in iOS. For example -
NSUserDefaults
- for accessing it laterBut for the simple scenario of passing a value to a different class whose allocation is done in the current class, the most common and preferred method would be the direct setting of values after allocation. This is done as follows:-
We can understand it using two controllers - Controller1 and Controller2
Suppose in Controller1 class you want to create the Controller2 object and push it with a String value being passed. This can be done as this:-
In the implementation of the Controller2 class there will be this function as-
You can also directly set the properties of the Controller2 class in the similar way as this:
To pass multiple values you can use the multiple parameters like :-
Or if you need to pass more than 3 parameters which are related to a common feature you can store the values to a Model class and pass that modelObject to the next class
So in-short if you want to -
Hope this helps