The way that IOS uses block reverse transfer value is detailed _ios

Source: Internet
Author: User


In an explanation of the way iOS passes through the proxy reverse value, analysis of how to use the proxy mode to reverse the value, in fact, there are other ways, such as notification, block, compared to agents, I personally think it is simpler, but need to deal with the details of the problem, such as block circular reference. Or use the previous case, the use of block to achieve, block the basic knowledge of this article no longer repeat.

I. Writing specifications

Block pass value, it should be noted that, who is the value of the need to define block, the capture only need to pass the block to pass the party, and processing the captured values.

Pass Value Square

1. Define block for passing values

2, declare a block attribute above, the specific implementation of this property needs to capture the party to pass in

3, when the need to pass the value of the call block to complete the transfer value

Capture side

1, pass a block to pass the value square

2. Capture the passed value in block and handle the captured value according to the requirement

Second, block and reverse transfer value

Or that sentence no Code, no BB, the case results are as follows:

Reverse Transfer value

Third, the realization step

1, transfer value side

. h file

 * Type customization
/typedef void (^returnvalueblock) (NSString *strvalue);

@interface Nextviewcontroller:uiviewcontroller
 * Declares a Returnvalueblock property, which is the interface that gets the value of the transfer
@property (nonatomic, copy) Returnvalueblock Returnvalueblock;



//.m file

#import " NextViewController.h "

@interface nextviewcontroller ()

@property (weak, nonatomic) Iboutlet Uitextfield * Inputtext;

-(Ibaction) Back: (ID) sender;


@implementation Nextviewcontroller

-(void) viewdidload {

  [super viewdidload];

  Self.navigationItem.title = @ "Second interface";

 * Return to previous interface
 * *
 @param sender button/
-(ibaction) Back: (ID) Sender {

  NSString * InputString = Self.inputText.text;

  if (self.returnvalueblock) {
    //send its own value out, complete the transfer value
    self.returnvalueblock (inputstring);

  [Self.navigationcontroller Popviewcontrolleranimated:yes];



2, the capture side

. m file

#import "ViewController.h"
#import "NextViewController.h"

@interface Viewcontroller ()

@ Property (weak, nonatomic) Iboutlet Uilabel *nextpassedvalue;

-(Ibaction) Next: (ID) sender;


@implementation Viewcontroller

-(void) viewdidload {

  [super Viewdidload];


Click the button to jump to the second interface
-(ibaction) Next: (ID) Sender {

  Nextviewcontroller *NVC = [[Nextviewcontroller alloc]init];

  Assignment block and assigns the captured value to Uilabel
  nvc.returnvalueblock = ^ (NSString *passedvalue) {

    Self.nextPassedValue.text = Passedvalue;


  [Self.navigationcontroller PUSHVIEWCONTROLLER:NVC animated:yes];



The above is the entire content of this article, I hope the content of this article for your iOS developers can help, if you have questions you can message exchange.

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: 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.