In this article, refer to the video from instructor Yan Jie.
When using block, you must declare and instantiate the block first.
BLOCK statement:
Block is divided into several situations: return values, parameters, no return values, no return values, and no parameters
INT (^ argreturnblock) (INT, INT) with return values and parameters );
The other types are int (^ returnblock )();
Void (argblock) (INT, INT );
Void (nonblock )();
Then instantiate
For example:
INT (INT argreturnblock) (INT, INT );
Argreturnblock = ^ (INT V1, int V2 ){
Return V1 + V2;
};
Finally, call
Int result = argreturnblock (13, 34 );
//////////////////////////////////////// /////////////////
Example of block callback;
Whether the listener button has been clicked
Mybutton * BTN = [[mybutton alloc] init]; BTN. buttonblock = ^ (mybutton * testbtn) {nslog (@ "% @ ___ the store", testbtn) ;}; [BTN click];
When the click method is called, it will be called back to the block method above.
#import <Foundation/Foundation.h>@class MyButton;typedef void(^listenBlock)(MyButton *sender);@interface MyButton : NSObject@property (nonatomic,assign) listenBlock buttonBlock;- (void)click;@end
In
#import "MyButton.h"@implementation MyButton- (void)click{ _buttonBlock(self);}@end
When the click method is called,
Will jump to the click method in mybutton. M, which contains _ buttonblock (Self );
It will jump to the place where the _ buttonblock declaration is made, that is, the place where the block is instantiated outside.
Block usage:
- Callback processing when the task is completed
- Message listener callback Processing
- Error callback Processing
- Enumeration callback
- View animation and Transformation
- Sort
- Http://blog.csdn.net/jasonblog/article/details/7756763
- For details, refer to the content in this link.
How to store blocks,
Block I know