iOS Development: Use block to pass values between two interfaces (Block advanced usage: Block pass value)

Source: Internet
Author: User

There are many places to use blocks, where the value is just a small part of it, and the following is a description of the value of block between two interfaces:

Let's talk about thought:

First, create two view controllers, create a Uilabel and a uibutton in the first view controller, where Uilabel is to display the string passed by the second view controller, UIButton is to push to the second interface.

The second interface has only one Uitextfield, is to enter the text, when the input text, and return to the first interface, when the second view is going to disappear, the second interface on the textfiled text to the first interface, and displayed on the Uilabel.

In fact, the core code on a few lines of code:

Here's the main code: (Because I'm a project created with storyboard, so the above properties and the corresponding method are using the system-generated outlet)

One, declare the Block property in the. h file of the second view controller

123456789 typedefvoid (^ReturnTextBlock)(NSString *showText); @interface TextFieldViewController : UIViewController @property (nonatomic, copy) ReturnTextBlock returnTextBlock; - (void)returnText:(ReturnTextBlock)block;@end

The first line of code is to redefine a name for the block to be declared.

1     ReturnTextBlock

In this way, the following will be very convenient when used.

The third line is a block property defined

Line four is a function that passes in a block statement in the first interface, but it doesn't have to be, but plus it reduces the amount of code written.

Ii. methods for implementing the second view controller

123456789 - (void)returnText:(ReturnTextBlock)block {    self.returnTextBlock = block;}- (void)viewWillDisappear:(BOOL)animated {        if(self.returnTextBlock != nil) {        self.returnTextBlock(self.inputTF.text);    }}

Where Inputtf is the Uitextfield in the view.

The first method is to define the method, save the incoming block statement block to this class instance variable Returntextblock (. h defined in the attribute), and then look for a timing call, and this time is said above, when the view will disappear, need to rewrite:

1 - (void)viewWillDisappear:(BOOL)animated;

Method.

Third, get the second view controller in the first view, and invoke the defined property with the second view controller

Write in the following ways:

12345678910 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.    TextFieldViewController *tfVC = segue.destinationViewController;        [tfVC returnText:^(NSString *showText) {        self.showLabel.text = showText;    }];}



You can see the comments in the code, and the system tells us that we can use [segue Destinationviewcontroller] to get a new view controller, which is what we call the second view controller.

This time the method defined above (in the first step) works, if you write a [TfVC returntext press ENTER, the system will automatically prompt out one:

TfVC returntext:<#^ (nsstring *showtext) block#>

, we can quickly create a block of code by simply returning to the focus, and you can try it. This is very handy when writing code.

Transferred from: Http://winann.blog.51cto.com/4424329/1438480?utm_source=tuicool

iOS Development: Use block to pass values between two interfaces (Block advanced usage: Block pass value)

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.