Block I know

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.