Pass value on ios page

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.