Block provides us with a very convenient way to implement various values and callbacks
Reasonable use of block reduces the amount of code and more elegant implementation capabilities
Now do a small collation as follows:
#pragma mark about block//block's prototype: NSString * (^myblock) (int);
The code above declares a block (^) prototype, named Myblock, that contains an int parameter, and returns a pointer to the NSString type.
Block definition: myblock=^ (int parama) {return [NSString stringwithformat:@ "passed number:%d", Parama];
};
Myblock (2); #pragma mark e.g.
Block Create and Usage//basic Use return value UIImage object UIImage *oldimage=[uiimage imagenamed:@ "Avatar"];
UIImage * (^myimageblock) (uiimage*) =^ (UIImage *image) {return image;
};
UIImage *newimage=myimageblock (oldimage);
Basic use of no return value nsstring *string=@ "";
void (^logblock) (NSString *) =^ (NSString *paramstr) {NSLog (@ "Logblock log:%@", paramstr);
};
Logblock (string); Functions that contain block parameters are more common types, such as Successfulblock//method in network requests-(void) Turnnumberwithnumbera: (Nsinteger) a numberb: (Nsinteger) b
Andmyblock: (void (^) (int aa,int bb)) myblock{int c=[[nsnumber Numberwithinteger:a]intvalue]; int D=[[nsnuMber Numberwithinteger:b]intvalue];
Myblock (C,D);
//Call [self turnnumberwithnumbera:1 numberb:2 andmyblock:^ (int aa, int bb) {NSLog (@ "AA is%d", AA);
NSLog (@ "BB is%d", BB);
}]; Block returns the value of TextField to a//b.h typedef void (^returntextblock) (NSString *showtext) when the value of two views Apush to b b disappears; Easy to invoke @interface B:uiviewcontroller @property (nonatomic, copy) Returntextblock returntextblock;//declares a B Lock Property-(void) Returntext: (returntextblock) block;//plus after easy to book a read the Block method @end//B.M-(void) R
Eturntext: (returntextblock) block {//block Implementation method Self.returntextblock = Block; }-(void) Viewwilldisappear: (BOOL) Animated {if (Self.returntextblock!= nil) {Self.retu
Rntextblock (Self.inputTF.text)//When the view will disappear call}//A.M call B *BVC = [[b alloc]init] when push is required; [BVC returntext:^ (NSString *showtext) {//define B view to invoke block SelF.showlabel.text = Showtext;
}];