Table of Contents: This article will be divided into three sections to explain the basic usage of block blocks, because I have limited level, deficiencies are still looking correct, thank you!
First, the basic usage of block blocks:
I believe everyone is very familiar with, the following directly on the code:
//1,block, no parameter, no return value void(^myblock) (void) = ^(void) {NSLog (@"2, print out, I am block, no parameter, no return value"); }; NSLog (@"1"); Myblock (); //2, with parameters, no return value void(^myblocktwo) (inta) = ^ (inta) {NSLog (@"@ =%d I am the block with parameters, no return value", a); }; Myblocktwo ( -); //3, with parameters, with return values int(^myblockthree) (int,int) = ^int(intAintb) {NSLog (@"%d I am the block, there are parameters, there are return values", A +b); returnA +b; }; Myblockthree ( A, About);
The above three is the most basic usage of block, but also local block blocks, not much to say, this main contention for beginners, the idea of block callback timing.
Second, block block of the advanced usage:
My code is in the implementation file
Statement:
-(void) Getnameblock: (void (^) (nsstring *string)) Myblockname;
Realize:
-(void) Getnameblock: (void (^) (NSString *)) myblockname{
NSLog (@ "I'll go First, ha");
[Self latergoblock:myblockname];
}
Call:
[Self getnameblock:^ (nsstring *string) {
NSLog (@ "This time my little Magochen gone ha,%@", string);
}];
Look at this time I define a block, the block is global, in the. h file is defined as follows:
Statement:
-(void) Latergoblock: (void (^) (NSString *)) MYBLOCKJC;
Realize:
-(void) Latergoblock: (void (^) (NSString *)) myblockjc{
MYBLOCKJC (@ "I am brother Pony, know my purpose");
}
See my goal is to implement block callback in this method, if this you understand, then the different interface with block pass value is very simple. Not to be continued
Simple use of IOS block