Simple use of iOS base _block language

Source: Internet
Author: User

When I started learning iOS, I didn't know how to touch the block language, and the more we used it, the more we had to learn, the more difficult it was to understand, and then I made an example and slowly understood, www.ios5.online not talk nonsense, the code:

The normal simple declaration of a block language is this:

Statement

Int (^yxpblock) (int, int) =^ (int a) {return a*a;};

Stating: The return value (^ The name of the language) (the type of the reference) =^ (the reference) {the main body};

Adjust

int result = Square (5);

I built a test that contained two categories (Yxpappdelegate and Yxpviewcontroller)

In the Yxpviewcontroller category

Declares a block language type

@classyxpViewController;

typedef void (^yxpblock) (Yxpviewcontroller *);

In Yxpviewcontroller, the Yxpblock was declared.

@interface Yxpviewcontroller:uiviewcontroller

@property (nonatomic,assign) Yxpblock Yxpblock;

@end

In Touchesbegan:withevent: Adjust block language (use a line for block language, @propert offer)

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

_yxpblock (self);

}

Block language used in Yxpappdelegate

__blocknsstring *[email protected] "touch screen";

Yxpviewcontroller *viewcontro=[[yxpviewcontrolleralloc] init];

viewcontro.yxpblock=^ (Yxpviewcontroller *viewcontroller) {

NSLog (@ "---------%@", str);

};

Self.window.rootviewcontroller=viewcontro;

Note: The block language can read external variables, but cannot be written in, and if you need to modify the external variables in the block language, the external changes are marked with __block (two lines)

To understand this example, believe it, you have a preliminary understanding of the block language. Read more: http://www.ios5.online/ios/iosjc/iosjczs/201703/40770.html

Simple use of iOS base _block language

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.