What is the block callback function in iOS

Source: Internet
Author: User

1. callback function similar to notification, delegate

2, callback function declaration, implementation, callback can be in a class, you can also write the callback to another class

3, declare a function in a class, in the implementation function, to meet the conditions, callbacks, in Class B, the callback function will be called, and to deal with the conditions of the things

For example, if the download is complete, trigger a callback function, notify the callback function in Class B, pass a download complete parameter, B class callback function will execute the corresponding code according to the parameters, if the success of how, if the failure of how to do, B class in the Class A download can do his own things.

4. Example (pseudo code):

declaration in Class A A.H

-(void) DownloadFile: (nsstring *) URL withstatus: (void (^) (BOOL status)) isfiledownloaded;

URL to downloadfile download provided URL parameters, this should be no doubt, that is, the parameters of the ordinary function, the following withstatus is a callback function, and then we implement the callback and see how to write this argument, void (^) (BOOL Status) is a modifier callback function isdownloaded, remember that is decorated callback function, void (^) Declaration of this callback function does not need return value, BOOL Status declaration callback function parameter is a bool type parameter status, tell the Class B callback function, Whether I succeeded or failed in the download.

Implemented in Class A a.m.

-(void) DownloadFile: (nsstring *) URL withstatus: (void (^) (BOOL)) isfiledownloaded{//download file according to URL ... if (downloade    d) {//If the download succeeds, trigger the callback function, pass the Yes parameter isfiledownloaded (yes);    } else {//If download fails, trigger callback function, pass no parameter isfiledownloaded (NO); }    }

In class B B.M

Let's write the callback function, and it should all be understood here that the callback function in Class B is called after the download is complete.

[Self downloadfile:@ "http://xxx.com/xxx.dmg" withstatus:<#^ (BOOL status) isfiledownloaded#>];

In

<#^ (BOOL stateus) isfiledownloaded#>

Enter on the top to expand

[Self downloadfile:@ "ddd" withstatus:^ (BOOL status)    {if (status) {...}} else {...} }];

In the function to determine whether the status is yes or no, to deal with different results!


What is the block callback function in iOS

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.