Pass value on ios page
Today, I checked that there are four methods to pass the value on the ios page:
Proxy delegateblock notification EXAMPLE class
I tried the first three methods today and recorded them here.
The following example shows two pages. Each page has a Button. click the Button on the first page to callback to the second page, and then click the second page to redirect to the first page, the title of the first button is changed to the value returned by the second button.
Proxy delegate
Proxy seems to be my heart. I can use APIs, but I can't write them myself. This is why I can write values today.
If two pages are passed in, where should the Protocol class be written and the proxy be defined on that page?
In conclusion, I think we can think like this: the second page wants to modify the value of the first page, but he can't do it, so he needs a proxy to help it modify, therefore, the proxy must be defined on the second page. The first page must support proxy.
The first page:
@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]; // obtain the second page from the storyboard: UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ Main bundle: [NSBundle mainBundle]; realendvc = [storyboard restart: @ second]; // sets the proxy realendvc. delegate = self;} // click the 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
- (void) changebutton:(NSString *)string;@end@interface realendVC : UIViewController@property (weak, nonatomic) IBOutlet UIButton *realendBtn;@property (nonatomic,weak) id
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: @ success]; }}@ end
Block
First page
#import
#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]; // obtain the second page from the storyboard: UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ Main bundle: [NSBundle mainBundle]; realendvc = [storyboard restart: @ second]; // allow access to variables in the block but cannot be modified. Add the following code _ block UIButton * button = _ frontBtn; realendvc. myblock = ^ (NSString * string) {[button setTitle: string forState: UIControlStateNormal] ;}// click Event-(IBAction) click :( id) sender {[self. navigationController pushViewController: realendvc animated: YES];} @ end
Second page
#import
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
Notification
First page
#import
#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]; // obtain the second page from the storyboard: UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ Main bundle: [NSBundle mainBundle]; realendvc = [storyboard restart: @ second]; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (change :) name: @ buttonobject: nil] ;}// notification call event-(void) change :( NSNotification *) notification {[_ frontBtn setTitle :( NSString *) [notification object] forState: UIControlStateNormal];} // click Event-(IBAction) click :( id) sender {[self. navigationController pushViewController: realendvc animated: YES];} @ end
Second page
#import
@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]; [[nsicationcenter center defacenter center] postNotificationName: @ button object: @ success];} @ end