The way IOS passes values between two pages via block _ios

Source: Internet
Author: User

First, functional requirements

In the first page there is a button and a label,label on the default display "haha", click the button to enter the second page. On the second page there is a Uitextfield and a button2, click Button2 to go back to the first page, but at the same time the text displayed on the label on the first page changes to the text just written in Uitextfield.

Second, the first definition block

On the page you want to pass. Define the method that contains the block parameter, which is defined in the. h file in the second page:

redefinition: typedef void (^returntextblock) (NSString *showtext);

Redefine the block class name void return value type Returntextblock class name (rename class name) NSString *showtext parameter

Declares a block variable: @property (nonatomic, copy) Returntextblock Returntextblock;

Note: The copy attribute is required here because block is first placed on the stack and only after copy is placed on the heap.

Block Invocation method:-(void) Returntext: (returntextblock) block;

Implementing in the. m file

Block's Calling method implementation code:-(void) Returntext: (returntextblock) block {

Self.returntextblock = block;

}

At this point, block preparation is complete.

Third, through the block in the two pages between the value of the transfer

On the first page, click on the button button to implement the Block method that jumps to the second page and invokes the second page.

-(void) firstbtnpressed

{

//blockself modifier Blockself.label avoids the block inside _label being circularly referenced

__weak Viewcontroller * Blockself = self;

Showviewcontroller *ordervc=[[showviewcontroller Alloc]init];

Block return value (very similar to the proxy notation, that is, the syntax is different, the agent is here is self.delegate=self;)

[ORDERVC returntext:^ (NSString *showtext) {

Blockself.label.text=showtext;

}];

[Self PRESENTVIEWCONTROLLER:ORDERVC animated:yes completion:nil];

}

How to implement the Button2 button in the second page. m file

-(void) secondbtnpressed

{

//As long as __block is added in front of the variable, the value of the variable can be modified in the block. Of course, there are other methods such as adding static.

[self dismissviewcontrolleranimated:yes completion:^{

//The block pointer needs to be processed before using block.

//No empty direct use, once the pointer is empty directly generated crash.

if (self.returntextblock!= nil) {

self.returntextblock (self.text.text);

NSLog (@ "text==%@", Self.text.text);

}

];}

In this way, we can achieve the functionality we want to achieve, very simple.

Summary

Who wants to pass the value who defines the method that contains the parameters of the block, calls the Blcok within the method, and the parameter to be passed gives the Blcok,blcok the ' place ' execution code to implement the code it wants to execute. Pass Value complete

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain 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: 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.