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 ~