In the previous blog "Introduction to iOS block", focusing on the concept of iOS block, this article will focus on their application in the 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 the words of Wikipedia, block is an attribute added by Apple Inc. for C, C + +, and objective-c, allowing these languages to create closures using the syntax of a class lambda expression. With regard to closures, one sentence is simple and straightforward: closures are functions that can read other functions ' internal variables.
In iOS development, block has many uses, and the interface reverse value is one of them.
If we have a demand as follows: interface A has a button, a label. From interface a jump to interface B, enter the string in the input box of interface B and display it on the label of interface A. This is a typical example of a reverse value. The core of this example is: "Enter the string in the input box of interface B and display it on the label on interface A." The block implements the reverse pass value.
1. Declare the Block property in the. h file of the second view controller:
// Define block void (^nextviewcontrollerblock) (NSString *tftext);
@interface*Inputtf; @end-(ibaction) Btnaction: (ID) sender { // determine if block is empty If (self. Nextviewcontrollerblock) {self . Nextviewcontrollerblock (Self.inputTF.text); } [Self.navigationcontroller Popviewcontrolleranimated:yes];}
2. Get the second view controller in the first view and invoke the defined properties with the second view controller:
@interface*Nextvcinfolabel; @end
-(Ibaction) btnclicked: (ID) sender { *NEXTVC = [[Nextviewcontroller alloc]init]; = ^ (NSString *tftext) { = tftext; }; [Self.navigationcontroller PUSHVIEWCONTROLLER:NEXTVC animated:yes];}
Effect
As this article is using Xib, omitted some UI details, attached to the code of this article link: source.
Where to write bad, hope to comment on the guidance. thanked ~
IOS Block Interface Reverse Pass value