I have been battling with passing data between two view controllers for a couple of days now and getting very confused. I'm new to Objective-C and finding some parts tricky to get my head round.
I have a Navigation Controller, FirstView is a form and on this form I have a button which loads SecondView which contains a TableView for the user to select some options. I then want to pass the selection back to the FirstView controller and display the data etc...
I have read alot about this (stackoverflow, iphonedevsdk, CS 193P Resources) and the options i've seen are,
1) ivar in app delegate (dirty and not recommended) 2) create a singleton 3) create a data model class 4) Use protocols and delegates (recommended by apple)
I want to do things right and want to use option 4 - Delegates in my program
Problem is, I don't understand delegates and how to setup and implement them.
Could anyone provide a basic example on how to setup and pass an NSArray using the delegate and 2 view controllers.
Thanks in advance Matt
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
You can use storyboard its quite easy. Use this in the implementation of
SecondViewController
and create a property inVIewController.h
(The first view's controller) named dataFromSecondViewThe same question has been answered over here:
Pass a variable between lots of UIVIewControllers
Hope this will be the easy & sufficient one .....
Off top of my head. You can replace
_returnedProperty
with your custom object and insetReturnedProperty
method do all the magic before actually assigning the checked value from the table.Delegation is the correct pattern to be used in this case, but your description doesn't look much like delegation as it is using a global variable. Perhaps you're storing global variables in your App Delegate which you should always try to avoid.
Here's a rough outline of what the code should look like:
SecondViewController.h:
SecondViewController.m:
FirstViewController.h:
FirstViewController.m: