The iOS Block interface reversely transmits values, and the iosblock interface reversely transmits values.

Source: Internet
Author: User

The iOS Block interface reversely transmits values, and the iosblock interface reversely transmits values.

 

In the previous blog "Introduction to iOS Block", I focused on analyzing the concept of iOS Block. This article will focus on their applications in development.

Block is an extension of the C language introduced by iOS4.0 + and Mac OS X 10.6 + to implement the features of anonymous functions.
In Wikipedia, Block is a feature that Apple Inc. adds to C, C ++, and Objective-C, allowing these languages to create closures using the syntax of lambda-like expressions. The closure statement is concise and clear: the closure is a function that can read internal variables of other functions.

In iOS development, Block has many functions, and reverse interface value transfer is one of them.

Suppose we have A requirement as follows: there is A button and A label on interface. Jump from interface A to interface B, enter A string in the input box of interface B, and display it on the label of interface. This is a typical example of reverse value transfer. The core of this example is: "enter A string in the input box of interface B and display it on the label of interface ". Block implements reverse value transfer.

 

1. Define Block attributes in the. h file of the second view controller:

// Define block @ property (nonatomic, copy) void (^ NextViewControllerBlock) (NSString * tfText );

 

@ Interface NextViewController () @ property (weak, nonatomic) IBOutlet UITextField * inputTF; @ end-(IBAction) BtnAction :( id) sender {// determine whether the block is empty if (self. nextViewControllerBlock) {self. nextViewControllerBlock (self. inputTF. text);} [self. navigationController popViewControllerAnimated: YES];}

 

 

2. Obtain the second view controller from the first view and use the second view controller to call the defined attributes:

@interface AViewController ()@property (weak, nonatomic) IBOutlet UILabel *nextVCInfoLabel;@end

 

- (IBAction)btnClicked:(id)sender {        NextViewController *nextVC = [[NextViewController alloc]init];    nextVC.NextViewControllerBlock = ^(NSString *tfText){        self.nextVCInfoLabel.text = tfText;    };        [self.navigationController pushViewController:nextVC animated:YES];}

 

 

Effect

 

 

Because XIB is used in this article, some UI details are omitted, and the code link of this article is attached: source code.

I hope you can comment on the problem. Thank you ~

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.