Transfer value between multiple VCs
1) Forward Pass value
When AVC launches BVC, a gives a value of B is called forward value
2) Forward Pass value implementation steps
STEP1:BVC to provide a public property
// exposes a property for receiving strings that are otherwise out of class
@property (nonatomic, copy) NSString *message;
STEP2:AVC assign values to properties exposed by BVC before the BCC is rolled out
//1. Create the BVC instances of
bviewcontroller *BVC = [[bviewcontrolleralloc] Initwithnibname:@ "Bviewcontroller"bundle:Nil ];
//2. to be BVC property assignments that are exposed
BVC. message = Self . TextField . text ;
//3. Launch
[selfpresentviewcontroller: BVC animated: YEScompletion:nil];
STEP3:BVC at a suitable time to show the value of AVC coming
-(void) viewwillappear: ( BOOL ) animated{
[superviewwillappear: animated];
self. label . text = Self . message ;
}
3) Reverse transmit value (callback)
AVC introduced BVC, in the process of B return to A, b to a pass value is called the reverse value
Solution One:
Step1: Adds a property for AVC to receive the returned data
// exposes a property that is used to receive B return value of the callback
@property (nonatomic, copy) NSString *backmessage;
Step2: Adds a property for BVC to receive AVC references
// exposes an attribute that is used to store AVC the reference
@property (nonatomic,strong) Aviewcontroller *AVC;
STEP3:AVC assigns itself to BVC-exposed properties before launching BVC
#import "AViewController.h"
#import "BViewController.h"
@interfaceaviewcontroller ()
@property (weak,nonatomic) Iboutlet UILabel *label;
@end
@implementation Aviewcontroller
-(void) viewdidload
{
[superviewdidload];
}
-(void) viewwillappear: ( BOOL ) animated{
[superviewwillappear: animated];
self. label . text = Self . Backmessage ;
}
-(ibaction) GOTOBVC: ( ID ) Sender {
bviewcontroller *BVC = [[bviewcontrolleralloc] Initwithnibname:@ "Bviewcontroller"bundle:Nil ];
//AVC His references to BVC .
BVC. AVC = Self ;
[selfpresentviewcontroller: BVC animated:YES completion: nil];
}
STEP4:BVC before dismiss, assigns the value of the text box to the stored AVC
STEP5:AVC displays the value of the callback in the Viewwillappear
@interfacebviewcontroller ()
@property (weak,nonatomic) Iboutlet Uitextfield *textfield;
@end
@implementation Bviewcontroller
-(void) viewdidload
{
[superviewdidload];
}
-(ibaction) GOBACKAVC: ( ID ) Sender {
self. AVC . Backmessage = Self . TextField . text ;
[selfdismissviewcontrolleranimated: YES Completion:nil];
}
Solution Two: Delegate
/*
1. Defining the Agreement
1 ) protocol naming: class name +delegate
2) name of method: try to reflect the timing of sending messages
3 the parameters of the method: the first parameter must be the principal's own, and the subsequent parameters can be the secondary information sent to the agent by the principal party.
2. Define a property that stores a reference for the agent
3. send a message to the agent at the right time
*/
/*
in the process of returning values, Inputviewcontroller as the principal party, Aviewcontroller as an agent
1. compliance with agreements
2. Implementation Methods
3. Set yourself as an agent
*/
M beef C Original Blog--mvc value, reverse pass value