=> Here Taken One Example ViewControllerA and ViewControllerB
=>For Here Used pass a BOOL value from ViewControllerA to ViewControllerB:-.
In ViewControllerB.h declared one property for the BOOL variable:
@property(nonatomic) BOOL *isSomethingEnabled;
In ViewControllerA you need to Say ViewControllerB so use It:-
#import "ViewControllerB.h"
=> Then where you want to load the view eg. didSelectRowAtIndex or some IBAction
you need to set the property in ViewControllerB before you push it onto nav stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
==> Here Passing Data Forward Using The Segue's:-
==> 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:-
=> Here Used One Of the Method Whitch is:-
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
==> Here Pass a BOOL from ViewControllerA to ViewControllerB:-
In ViewControllerB.h create a property for the BOOL
@property(nonatomic) BOOL *isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB so use an
#import "ViewControllerB.h"
=> Create a the segue from ViewControllerA to ViewControllerB 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 thats performed we will pass our BOOL value to ViewControllerB:-
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showDetailSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;
}
}
=>If you have your views embedded 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 isEqualToString:@"showDetailSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
controller.isSomethingEnabled = YES;
}
}
=> This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
==> Passing Data Back
In ViewControllerB.h, below the #import, but above @interface you specify the protocol.
@class ViewControllerB;
@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
=> Next still in the ViewControllerB.h you need to setup a delegate property and
synthesize in ViewControllerB.m
@property (nonatomic, weak) id <ViewControllerBDelegate> delegate;
=> In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
=> That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to
import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
@interface ViewControllerA : UIViewController <ViewControllerBDelegate>
=> In ViewControllerA.m implement the following method from our protocol
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
NSLog(@"This was returned from ViewControllerB %@",item);
}
=> We need to tell ViewControllerB that ViewControllerA is its delegate before
we push ViewControllerB on to nav stack:-
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];
=>For Here Used pass a BOOL value from ViewControllerA to ViewControllerB:-.
In ViewControllerB.h declared one property for the BOOL variable:
@property(nonatomic) BOOL *isSomethingEnabled;
In ViewControllerA you need to Say ViewControllerB so use It:-
#import "ViewControllerB.h"
=> Then where you want to load the view eg. didSelectRowAtIndex or some IBAction
you need to set the property in ViewControllerB before you push it onto nav stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
==> Here Passing Data Forward Using The Segue's:-
==> 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:-
=> Here Used One Of the Method Whitch is:-
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
==> Here Pass a BOOL from ViewControllerA to ViewControllerB:-
In ViewControllerB.h create a property for the BOOL
@property(nonatomic) BOOL *isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB so use an
#import "ViewControllerB.h"
=> Create a the segue from ViewControllerA to ViewControllerB 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 thats performed we will pass our BOOL value to ViewControllerB:-
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showDetailSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;
}
}
=>If you have your views embedded 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 isEqualToString:@"showDetailSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
controller.isSomethingEnabled = YES;
}
}
=> This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
==> Passing Data Back
In ViewControllerB.h, below the #import, but above @interface you specify the protocol.
@class ViewControllerB;
@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end
=> Next still in the ViewControllerB.h you need to setup a delegate property and
synthesize in ViewControllerB.m
@property (nonatomic, weak) id <ViewControllerBDelegate> delegate;
=> In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
=> That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to
import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
@interface ViewControllerA : UIViewController <ViewControllerBDelegate>
=> In ViewControllerA.m implement the following method from our protocol
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
NSLog(@"This was returned from ViewControllerB %@",item);
}
=> We need to tell ViewControllerB that ViewControllerA is its delegate before
we push ViewControllerB on to nav stack:-
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];