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];
}