Before on the StackOverflow saw a preach value (Segue and delegate value) of the article, feel speak very clear, will delegate part translated a bit, interested can see.
Original:
Http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers
President
to get fromViewcontrollerbThe value is returned toViewcontrollera, we need to use protocols(Protocols)and Agents(Delegates).
to implement this process, we need to set viewcontrollera viewcontrollerb viewcontrollerb can send messages to viewcontrollera
viewcontrollera as viewcontrollerb Agent must obey us in viewcontrollerb ( protocols viewcontrollera What are the methods that must be implemented.
1. Span lang= "ZH-CN" style= "font-size:11pt" in viewcontrollerb.h #import and @interface
|
@class viewcontrollerb //Important
@protocol Viewcontrollerbdelegate <nsobject ;
-( void ) Additemviewcontroller: (viewcontrollerb *) Controller Didfinishenteringitem: ( Span style= "COLOR: #2B91AF" >nsstring *) item; @end |
Note: (NSString *) Item is the type of data we want to return now, or it can be other types, such as dictionaries, arrays, etc.
2. still in ViewControllerB.h , set a delegate property while Viewcontroller. M medium synthesize
|
@property (nonatomic, weak) ID <viewcontrollerbdelegate>delegate; |
This is what I did in the project:
@propertyID<selectpeoplevcdelegate>delegate;
3.in theViewcontrollerb, we send a message to the agent when we are going to pop the view from the navigation controller(The message contains the value we want to pass.)
|
nsstring *itemtopassback = @"Pass This value, back to Viewcontrollera";
[Self. delegate Additemviewcontroller:self Didfinishenteringitem:itemtopassback]; |
In the actual project I have done this:
-(void) viewdiddisappear: ( BOOL ) Animated
{
[self. Delegate additemviewcontroller:selfdidfinishselectpeople: Datasourcearray];
}
Note:datasourcearray is my data source, in a public variable, to complete the assignment in the previous program.
4.the above is all that is to beViewcontrollerband the next step is toViewcontrollerathe operation. First, we're going toViewControllerA.hImport inViewcontrollerb, and comply with its protocol:
|
#import "ViewControllerB.h"
@interface viewcontrollera :uiviewcontroller <viewcontrollerbdelegate> |
5.in theVIEWCONTROLLERA.MTo implement the Protocol method:
|
-(void) Additemviewcontroller: (viewcontrollerb *) controller didfinishenteringitem: (nsstring *) Item
{ NSLog (@"This is returned from Viewcontrollerb%@", item);
} |
6.Finally, before we willViewcontrollerbbefore pressing into the stack, we need to tellViewcontrollerb,Viewcontrollerait's a proxy.(delegate):
|
viewcontrollerb *viewcontrollerb = [[viewcontrollerb alloc] initwithnib:@"Viewcontrollerb" Bundle:nil];
Viewcontrollerb. delegate = self
[Self Navigationcontroller] pushviewcontroller:viewcontrollerb Animated:yes]; |
This is what I did in the actual project:
-(void) Prepareforsegue: ( Uistoryboardsegue *) Segue Sender: (ID) sender
{
uiviewcontroller * viewcontroller = segue. Destinationviewcontroller ;
BAGSELECTPEOPLEVC * Selectpeoplevc = (bagselectpeoplevc *) Viewcontroller;
SELECTPEOPLEVC. Delegate = Self ;
}
Delegate transfer value between iOS view Controllers tutorial