Today I looked at the way iOS pages are transmitted in roughly four ways:
- Agent Delegate
- Block
- Notice
- A single case class
Try the top three today and record it here
The following example is a two page, each page has a button, click on the first page of the button callback to the second page, and then click the second page back to the first page, the title of the first button becomes the value returned by the second button.
Agent Delegate
Agent seems to be my heart knot, can use the API but do not write their own, which is today will write the value of the reason.
Suppose two pages are passed, where should the Protocol class be written, and what should the proxy be defined on that page?
in summary, I think I can think of this: The second page wants to modify the value of the first page, but he can't do it, so he needs an agent to help it modify, so the agent needs to be defined on the second page. The first page requires a support agent .
First page:
@interface frontvc:uiviewcontroller<change> @property (weak, nonatomic) Iboutlet UIButton *frontbtn; @end
#import "FrontVC.h" @interface FRONTVC () { REALENDVC *realendvc;//second page} @end @implementation frontvc-(void) viewdidload { [super viewdidload]; Get a second page from storyboard uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:[nsbundle Mainbundle]]; REALENDVC = [Storyboard instantiateviewcontrollerwithidentifier:@ "second"]; Set proxy realendvc.delegate = self;} button click Event-(ibaction) Click: (ID) Sender { [Self.navigationcontroller PUSHVIEWCONTROLLER:REALENDVC animated:yes];} Proxy Method-(void) Changebutton: (NSString *) string{ [_frontbtn settitle:string forstate:uicontrolstatenormal];} @end
second page:
@protocol change <nsobject>-(void) Changebutton: (NSString *) string; @end @interface REALENDVC: Uiviewcontroller@property (Weak, nonatomic) Iboutlet UIButton *realendbtn; @property (nonatomic,weak) id<change> delegate; @end
#import "RealendVC.h" @interface REALENDVC () @end @implementation realendvc-(void) viewdidload { [Super Viewdidload ];} -(Ibaction) Click: (ID) Sender { [Self.navigationcontroller popviewcontrolleranimated:yes]; if (self.delegate) { [self.delegate changebutton:@ "succeeded"];} } @end
Block
First page
#import <UIKit/UIKit.h> #import "realendVC.h" @interface frontvc:uiviewcontroller@property (weak, nonatomic) Iboutlet UIButton *frontbtn; @end
#import "FrontVC.h" @interface FRONTVC () { REALENDVC *realendvc;//second page} @end @implementation frontvc-(void) viewdidload { [super viewdidload]; Get a second page from storyboard uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:[nsbundle Mainbundle]]; REALENDVC = [Storyboard instantiateviewcontrollerwithidentifier:@ "second"]; Allow access to variables in block, but do not allow modification, you need to add the following code __block UIButton * button = _frontbtn; Realendvc.myblock = ^ (NSString *string) { [button settitle:string forstate:uicontrolstatenormal];} ;} button click Event-(ibaction) Click: (ID) Sender { [Self.navigationcontroller PUSHVIEWCONTROLLER:REALENDVC animated:yes];} @end
a second page
#import <uikit/uikit.h>typedef Void (^myblock) (NSString *); @interface Realendvc:uiviewcontroller@property ( Weak, nonatomic) Iboutlet UIButton *realendbtn; @property (copy,nonatomic) myblock Myblock; @end
#import "RealendVC.h" @interface REALENDVC () @end @implementation realendvc-(void) viewdidload { [Super Viewdidload ];} -(Ibaction) Click: (ID) Sender { [Self.navigationcontroller popviewcontrolleranimated:yes]; Self.myblock (@ "Success");} @end
Notice
First page
#import <UIKit/UIKit.h> #import "realendVC.h" @interface frontvc:uiviewcontroller@property (weak, nonatomic) Iboutlet UIButton *frontbtn; @end
#import "FrontVC.h" @interface FRONTVC () { REALENDVC *realendvc;//second page} @end @implementation frontvc-(void) viewdidload { [super viewdidload]; Get a second page from storyboard uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:[nsbundle Mainbundle]]; REALENDVC = [Storyboard instantiateviewcontrollerwithidentifier:@ "second"]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (change:) name:@ "button" Object:nil]; } Notification invocation event-(void) Change: (Nsnotification *) notification{ [_frontbtn settitle: (NSString *) [Notification Object] Forstate:uicontrolstatenormal];} button click Event-(ibaction) Click: (ID) Sender { [Self.navigationcontroller PUSHVIEWCONTROLLER:REALENDVC animated:yes];} @end
a second page
#import <UIKit/UIKit.h> @interface realendvc:uiviewcontroller@property (weak, nonatomic) Iboutlet UIButton * REALENDBTN; @end
#import "RealendVC.h" @interface REALENDVC () @end @implementation realendvc-(void) viewdidload { [Super Viewdidload ];} -(Ibaction) Click: (ID) Sender { [Self.navigationcontroller popviewcontrolleranimated:yes]; [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "button" object:@ "Success"];} @end
Thank the bloggers for their great God.
iOS Page Pass value