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 haven't missed anything.
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 and ViewControllerB.
To pass a BOOL value from ViewControllerA to ViewControllerB, we would do the following.
1.In ViewControllerB.h create a propety for the BOOL.
@property (nonatomic) BOOL *isSomethingEnabled;
2.In ViewControlerA.h you need to tell it about ViewControllerB,you use an
#import "ViewControllerB.h"
Then where you want to load the view eg. didSelecteRowAtIndex or some IBAction you need to set the property in
ViewControllerB before you push it onto nav stack.
ViewControllerB *viewB=[[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewB.isSomethingEnabled=YES;
[self pushViewController:viewControllerB animated:YES];
If you are using Storyboards you are most likely using segues and will need this proceduce 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.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
So to pass a BOOL from ViewControllerA to ViewControllerB we would do the following:
First you have to implement the first 2 steps above in Passing Data Forward.
3.Create a seuge from ViewControllerA to ViewControllerB on the storyboard and give it an identifier, in this example we
will call it "showDetailSegue".
4.Next we need to add the method to ViewControlerA 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 thats performed
we will pass out BOOL value to ViewControllerB.
used as a loosely coupled mechanism for callbacks.
To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to
ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
For ViewControllerA to be delegate fo ViewControllerB it must conform to ViewControllerB's protocol which we have to specify.
This tells ViewControllerA which methods it must implement.
1.In ViewControllerB.h, below the #import, but above @interface you specify the protocol.
@class ViewControllerB;
@protocol ViewBDelegate<NSObject>
-(void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
2.Next still in the ViewControllerB.h, you need to setup a delegate property and synthesize in ViewControllerB.m;
@property(nonatomic,weak)id<ViewControllerBDeleage>delegate;
3.In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack=@"Pass this to back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnterItem:itemToPassBack];
4.That's for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
@interface ViewControllerA:UIViewController<ViewBDelegate>
5.In ViewControllerA.m implement the following method from our protocol.
-(void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item{
NSLog("This was returned from ViewControllerB %@",item);
}
6.The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav
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 haven't 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 and ViewControllerB.
To pass a BOOL value from ViewControllerA to ViewControllerB, we would do the following.
1.In ViewControllerB.h create a propety for the BOOL.
@property (nonatomic) BOOL *isSomethingEnabled;
2.In ViewControlerA.h you need to tell it about ViewControllerB,you use an
#import "ViewControllerB.h"
Then where you want to load the view eg. didSelecteRowAtIndex or some IBAction you need to set the property in
ViewControllerB before you push it onto nav stack.
ViewControllerB *viewB=[[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewB.isSomethingEnabled=YES;
[self pushViewController:viewControllerB animated:YES];
Passing Data Forward using Segue's
If you are using Storyboards you are most likely using segues and will need this proceduce 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.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
So to pass a BOOL from ViewControllerA to ViewControllerB we would do the following:
First you have to implement the first 2 steps above in Passing Data Forward.
3.Create a seuge from ViewControllerA to ViewControllerB on the storyboard and give it an identifier, in this example we
will call it "showDetailSegue".
4.Next we need to add the method to ViewControlerA 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 thats performed
we will pass out BOOL value to ViewControllerB.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([sugue.identifier isEquealToString:@"showDetailSegue"]){
ViewControllerB *viewB=(ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled=YES;
}
}
If you have your views embeded in a navigation controller you need to change the method above slightly to the following:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEquealToString:@"showDetailSegue"]){
UINavigationController *navController=(UINavigationController *)segue.destinationViewController;
ViewControllerB *viewB=(ViewControllerB *)navController.topViewController;
viewB.isSomethingEnabled=YES;
}
}
Passing Data Back
To pass data back from ViewControllerB to ViewControllerA, you need to use Protocols and Delegates or Blocks, the latter can beused as a loosely coupled mechanism for callbacks.
To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to
ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
For ViewControllerA to be delegate fo ViewControllerB it must conform to ViewControllerB's protocol which we have to specify.
This tells ViewControllerA which methods it must implement.
1.In ViewControllerB.h, below the #import, but above @interface you specify the protocol.
@class ViewControllerB;
@protocol ViewBDelegate<NSObject>
-(void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
2.Next still in the ViewControllerB.h, you need to setup a delegate property and synthesize in ViewControllerB.m;
@property(nonatomic,weak)id<ViewControllerBDeleage>delegate;
3.In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack=@"Pass this to back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnterItem:itemToPassBack];
4.That's for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
@interface ViewControllerA:UIViewController<ViewBDelegate>
5.In ViewControllerA.m implement the following method from our protocol.
-(void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item{
NSLog("This was returned from ViewControllerB %@",item);
}
6.The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav
stack.
ViewControllerB *viewB=[ViewControllerB alloc]iniWithNib:@"ViewControllerB" bundle:nil];
viewB.delegate=self;
[[self.navigationController] pushViewController:viewControllerB animated:YES];