Simple use of IOS block

Source: Internet
Author: User

Table of Contents: This article will be divided into three sections to explain the basic usage of block blocks, because I have limited level, deficiencies are still looking correct, thank you!

First, the basic usage of block blocks:

I believe everyone is very familiar with, the following directly on the code:

    //1,block, no parameter, no return value    void(^myblock) (void) = ^(void) {NSLog (@"2, print out, I am block, no parameter, no return value");    }; NSLog (@"1");        Myblock (); //2, with parameters, no return value    void(^myblocktwo) (inta) = ^ (inta) {NSLog (@"@ =%d I am the block with parameters, no return value", a);        }; Myblocktwo ( -); //3, with parameters, with return values    int(^myblockthree) (int,int) = ^int(intAintb) {NSLog (@"%d I am the block, there are parameters, there are return values", A +b); returnA +b;       }; Myblockthree ( A, About);

The above three is the most basic usage of block, but also local block blocks, not much to say, this main contention for beginners, the idea of block callback timing.

Second, block block of the advanced usage:

My code is in the implementation file

Statement:

-(void) Getnameblock: (void (^) (nsstring *string)) Myblockname;

Realize:

-(void) Getnameblock: (void (^) (NSString *)) myblockname{

NSLog (@ "I'll go First, ha");

[Self latergoblock:myblockname];

}

Call:

[Self getnameblock:^ (nsstring *string) {

NSLog (@ "This time my little Magochen gone ha,%@", string);

}];

Look at this time I define a block, the block is global, in the. h file is defined as follows:

Statement:

-(void) Latergoblock: (void (^) (NSString *)) MYBLOCKJC;

Realize:

-(void) Latergoblock: (void (^) (NSString *)) myblockjc{

MYBLOCKJC (@ "I am brother Pony, know my purpose");

}

See my goal is to implement block callback in this method, if this you understand, then the different interface with block pass value is very simple. Not to be continued

Simple use of IOS block

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.