Advanced Block Practice
One. The simplest block use
Three steps to use block: 1. Define the block variable 2. Creating block code blocks 3. Call Block anonymous function
Defining a block's composition includes: return value, block name, parameter type.
Block blocks of code as an anonymous function can be written in other methods, so we generally write block blocks of code in other methods, the block code blocks will not be executed when calling this method, only when the block blocks of code to be executed.
ViewController.h
1 #import<UIKit/UIKit.h>2 3 @interfaceViewcontroller:uiviewcontroller4 5@property (nonatomic,copy)void(^myblock) (int);//1. Declaring a block variable, it is more convenient and practical to declare a block directly as a property variable. 6 7 @end8 9 VIEWCONTROLLER.MTen One #import "ViewController.h" A - #import "NewViewController.h" - the @interfaceViewcontroller () - - @end - + @implementationViewcontroller - +- (void) Viewdidload { A at [Super Viewdidload]; - - //Note: The method of writing block code blocks is first loaded - - [self blocktest]; - in } - to-(void) blocktest{ + - //2. Write block blocks of code inside the method the *_myblock = ^ (inta) { $ Panax NotoginsengNSLog (@"%d", a); - the }; + A } the +-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ - $ //3. Call the block anonymous function $ -_myblock (Ten); - theNewviewcontroller * Newviewcontroller =[[Newviewcontroller alloc]init]; - WuyiNewviewcontroller.block =_myblock; the -[Self Presentviewcontroller:newviewcontroller animated:yes completion:^{ Wu - }]; About $ } - - @end
The above code, in the Viewcontroller implementation of the block implementation of the three steps, the function of the block is to print the parameter values.
The touch method implements a jump from Viewcontroller to Newviewcontroller, while the block within the Viewcontroller is Myblock assigned to the block defined in Newviewcontroller, Makes two blocks essentially the same block, so that in newviewcontroller it is possible to invoke the code block in Viewcontroller.
NewViewController.h
1 #import <UIKit/UIKit.h>23#import"ViewController.h " 4 5 @interface Newviewcontroller:uiviewcontroller 6 7 @property (nonatomic,copy)void(^block) (int a); 8 9 @end
Newviewcontroller.m
1 #import "NewViewController.h"2 3 @interfaceNewviewcontroller ()4 5 @end6 7 @implementationNewviewcontroller8 9- (void) Viewdidload {Ten One [Super Viewdidload]; A -Self.view.backgroundcolor=[Uicolor Graycolor]; - the } - --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ - +Self.block ( -); - +[Self Dismissviewcontrolleranimated:yes completion:^{ A at }]; - - } - - @end
Two. Block other definition types
Declare the block type with a typedef (because the block has no type, declare a type with a typedef). Two blocks with two finishedblock types are declared below.
1 #import<UIKit/UIKit.h>2 3typedefint(^finishedblock) (int);//here Finishedblock is not a block variable, but a type, the Finishedblock type. 4 5 @interfaceViewcontroller:uiviewcontroller6 7 {8 9Finishedblock Block1;//after a typedef declares a finishedblock, it is possible to define a block variable with that type. Ten One } A - @property (nonatomic,copy) finishedblock Block2; - the @end - - #import "ViewController.h" - + @interfaceViewcontroller () - + @end A at @implementationViewcontroller - -- (void) Viewdidload { - - [Super Viewdidload]; - inBlock1 = ^ (inta) { - toNSLog (@"%d", a); + - returnA; the * }; $ Panax Notoginseng_block2 = ^ (inta) { - theNSLog (@"%d", a); + A returnA; the + }; - $Finishedblock Block3 = ^ (inta) { $ -NSLog (@"%d", a); - the returnA; - Wuyi }; the -Block3 ( -);//Callback Wu - [self test]; About $ } - --(void) test{ - A //Callback + theBlock1 (Ten); - $_block2 ( -); the the } the the @end
Printing results:
2015-07-15 20:57:22.226 test[618:21849] 30
2015-07-15 20:57:22.227 test[618:21849] 10
2015-07-15 20:57:22.227 test[618:21849] 20
Three. Block as a parameter pass
1 #import<UIKit/UIKit.h>2 3typedefint(^finishedblock) (int);4 5 @interfaceViewcontroller:uiviewcontroller6 7 @end8 9 #import "ViewController.h"Ten One @interfaceViewcontroller () A - @end - the @implementationViewcontroller - -- (void) Viewdidload { - + [Super Viewdidload]; - +[Self testblock:^int(inta) { A atNSLog (@"%d", a); - - returnA; - - }]; - in } - to-(void) Testblock: (finishedblock) block{ + -Block +);//Callback the * } $ Panax Notoginseng @end
iOS Development-Grammar & Advanced Block Exercises