Closure (block), closure block
Block mainly solves the problem of reverse value transfer and value transfer.
1. Type of the return value of the block Statement (^ name) (parameter list );
2. block implementation formula name = ^ (parameter list ){};
3. Local variables define the block of an image name
Void (^ imageNameBlock) (NSString * imageName); // declare
_ Block UIImage * image; // iamge = nil if you want to modify the value of an external variable, add _ block. That is to say, if you want to change the state of an external variable within the block, _ block is required
4. Implement block
ImageNameBlock = ^ (NSString * imageName) {// implementation
Image = [UIImage imageNamed: imageName];
Self. view. backgroundColor = [UIColor colorWithPatternImage: image];
};
5. Call block
ImageNameBlock (@ "photo.png"); // call
Reverse value transfer:
1. Write block in the class to be passed, that is, declare
@ Property (nonatomic, copy) void (^ block) (NSString * content); // The block is allocated in the stack and does not need to be managed by programmers.
2. Implement block where values need to be received
NextViewController * nextView = [[NextViewController alloc] init];
NextView. block = ^ (NSString * content ){
NSLog (@ "% @", content );
LabelTextBlock (content );
};
3. Call block where the value needs to be transferred
Block (@ "sfasfaklgjgjkdsjkns ");