When I started learning iOS, I didn't know how to touch the block language, and the more we used it, the more we had to learn, the more difficult it was to understand, and then I made an example and slowly understood, www.ios5.online not talk nonsense, the code:
The normal simple declaration of a block language is this:
Statement
Int (^yxpblock) (int, int) =^ (int a) {return a*a;};
Stating: The return value (^ The name of the language) (the type of the reference) =^ (the reference) {the main body};
Adjust
int result = Square (5);
I built a test that contained two categories (Yxpappdelegate and Yxpviewcontroller)
In the Yxpviewcontroller category
Declares a block language type
@classyxpViewController;
typedef void (^yxpblock) (Yxpviewcontroller *);
In Yxpviewcontroller, the Yxpblock was declared.
@interface Yxpviewcontroller:uiviewcontroller
@property (nonatomic,assign) Yxpblock Yxpblock;
@end
In Touchesbegan:withevent: Adjust block language (use a line for block language, @propert offer)
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
_yxpblock (self);
}
Block language used in Yxpappdelegate
__blocknsstring *[email protected] "touch screen";
Yxpviewcontroller *viewcontro=[[yxpviewcontrolleralloc] init];
viewcontro.yxpblock=^ (Yxpviewcontroller *viewcontroller) {
NSLog (@ "---------%@", str);
};
Self.window.rootviewcontroller=viewcontro;
Note: The block language can read external variables, but cannot be written in, and if you need to modify the external variables in the block language, the external changes are marked with __block (two lines)
To understand this example, believe it, you have a preliminary understanding of the block language. Read more: http://www.ios5.online/ios/iosjc/iosjczs/201703/40770.html
Simple use of iOS base _block language