Inverse value between iOS pages

There are two kinds of values between pages:
  • Forward pass value (the value of the property can be used, very simple)
  • Reverse transfer value (there are 3 commonly used methods) proxy value block value transfer notification center value
This article is to share the value of the reverse of the 3 ways (please follow me to write a simple Demo Click to download)
1 proxy pass values (relative to block, agents are more suitable for implementing multiple methods)
  • The first step: the value declared in the NextViewcontroller agreement (also can create a kind of agreement) #import < UIKit/UIKit.h&gt @protocol; ChangeName / changeName: protocol (void) – (NSString*) string; @end @interface NextViewController: UIViewController
  • The second step: the ARC statement (memory automatic release mechanism), using modified weak; assign was used to modify the @interface NextViewController MRC environment: UIViewController @property (nonatomic, weak) id< ChangeName> delegate; / / agent
  • The third step: the proxy agent calls the method in the method of triggering button in the / / – (IBAction) delegateAction: (UIButton * sender) {if (“isEqualToString:@ [self.nameTextField.text!”) {[self.delegate changeName:self.nameTextField.text]; / / write in front of or written in the dismiss Block [self dismissViewControllerAnimated:YES completion:^{}] can};}
  • The fourth step: in the ViewController into the NextViewController header files, and comply with the protocol of #import “NextViewController.h” / / because I have written in the NextViewController agreement so as to pour into the header file for the @interface ViewController (ChangeName&gt) <
  • The fifth step: to set up a proxy (because I use the storyboard method in this so jump set proxy) I will write an article about the usage of storyboard article to talk about note after – (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (ID sender) {/ / let NextViewController example is NextViewController* vc=segue.destinationViewController segue target controller //segue; storyboard is the tow line vc.delegate=self;}
  • The sixth step: the implementation of protocol protocol method to realize the method / / in the ViewController.M file – (void) changeName: (NSString * string) {self.NameLabel.text=string};
2.block value

There are 4 ways to pass the value of block, where I provide a simple way to write, you can download a simple link in my Demo look, there are 2 ways

  • The first step: in the NextViewController (.H to ensure that at any time access) to declare a Block attribute with copy modified @property (nonatomic, copy) void (^change) (stringName) (NSString*) (); //block method
  • The second step: in the NextViewController.m file button trigger method call – (IBAction) blockAction: (UIButton * sender) {//block self.change (self.nameTextField.text) 1; / / write in front of or written in the dismiss Block [self dismissViewControllerAnimated:YES completion:^{can}];}
  • The third step: in the ViewController.m import header files and implementation of the block#import “NextViewController.h” – (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (ID sender) {/ / let NextViewController example is NextViewController* vc=segue.destinationViewController segue //segue is the target controller; in storyboard 1 //block writing __weak __typeof line (self) weakSelf = self; / / to prevent the vc.change=^ (NSString* stringName) circular reference {weakSelf.NameLabel.text=stringName};};
3 notification center value

I love this value, because it is easier and more convenient to use the notification center NSNotificationCenter is facing the global program to send messages, which can be received, but please note that the message name (not name)

  • The first step: add observation in ViewController – (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (ID sender) {/ / add defaultCenter] notification center observer [[NSNotificationCenter addObserver:self selector:@selector (changeLabelText:) name:@ “change the name” object:nil];}
  • The second step: in the ViewController.m implementation of the observer to receive the message after the implementation of the method – (void) changeLabelText: (NSNotification*) sender {“self.NameLabel.text=sender.userInfo[@”;}
  • The third step: to remove the observer / remove notification center in ViewController.m – (void) dealloc {[[NSNotificationCenter defaultCenter]removeObserver:self];}
  • The fourth step: in the NextViewController.m send a message / send notification notification Center – (IBAction) NSNotificationAction: (UIButton * sender) {NSDictionary* [email protected]{@ “Name: self.nameTextField.text} [[NSNotificationCenter defaultCenter] postNotificationName:@”; “change the name” object:nil userInfo:dic]; [self dismissViewControllerAnimated:YES completion:^{}];}

Note: I will continue to update the relevant content. If you want to find some iOS code book I can focus on Jane, I will continue to update, we work together to discuss this
thank you for reading