Pass value for IOS-pass value by proxy, block, nsicationicationcenter, nsicationicationcenter

Source: Internet
Author: User

Pass value for IOS-pass value by proxy, block, nsicationicationcenter, nsicationicationcenter

1. Use a proxy to transmit values, that is, use a proxy to communicate.

Interface file:

# Import <Foundation/Foundation. h>

 

@ Protocol Cdelegate <NSObject>

 

-(Void) change :( NSString *) name;

 

 

 

@ End

 

. H file

 

@ Interface ViewController: UIViewController <Cdelegate>

 

. M file

-(IBAction) pushBB :( id) sender {

BViewController * bc = [[BViewController alloc] initWithNibName: @ "BViewController" bundle: [NSBundle mainBundle];

Bc. delegate = self;

[Self presentViewController: bc animated: YES completion: nil];

 

}

 

BViewController File

. H file

# Import "ViewController. h"

# Import "Cdelegate. h"

 

 

@ Interface BViewController: ViewController

 

@ Property (weak, nonatomic) IBOutlet UITextField * name;

@ Property (nonatomic, assign) id <Cdelegate> delegate;

@ Property (nonatomic, copy) ablock block;

-(IBAction) popBB :( id) sender;

@ End

 

. M file

-(IBAction) popBB :( id) sender {

[Self. delegate change: self. name. text];

[Self dismissViewControllerAnimated: YES completion: nil];

}

Ii. block Value Transfer

Typedef void (^ ablock) (NSString * str );

@ Property (nonatomic, copy) ablock block;

 

-(IBAction) popBB :( id) sender {

// [Self. delegate change: self. name. text];

Self. block (self. name. text );

[Self dismissViewControllerAnimated: YES completion: nil];

}

 

 

 

-(IBAction) pushBB :( id) sender {

BViewController * bc = [[BViewController alloc] initWithNibName: @ "BViewController" bundle: [NSBundle mainBundle];

Bc. block = ^ (NSString * str ){

Self. aname. text = str;

};

[Self presentViewController: bc animated: YES completion: nil];

 

}

 

3. Notification

 

NSDictionary * dic = [NSDictionary dictionaryWithObject: self. name. text forKey: @ "name"];

[[Nsicationcenter center defacenter center] postNotificationName: @ "changeText" object: self userInfo: dic];

 

-(Void) viewDidLoad {

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

 

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (changeText :) name: @ "changeText" object: nil];

 

}

 

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.