Block Value passing

Source: Internet
Author: User

Defining block is the same as defining delegate. It is defined in the class on the page on which the expected value is obtained.

For example: Class A and Class B, if I want to pass the value in Class A to Class B, I will define block in Class A and Class B ....

  

For example:

Step 1:

In Class:

. H file:

# Import "baseviewcontroller. H"

// Define the block

Typedef void (^ addcommunity) (uiimage * IMG, nsstring * Title );

@ Interface moreviewcontroller: baseviewcontroller

@ Property (nonatomic, copy) addcommunity adds;

-(Void) addcommunitywithblock :( addcommunity) addblock;

. M file:

-(Void) viewdidload

{

[Super viewdidload];

// Do any additional setup after loading the view.

Imgview = [[[uiimageview alloc] initwithframe: cgrectmake (100,100,100,100)];

Nsstring * IMG = @ "market-android.png ";

Imgview. Image = [uiimage imagenamed: img];

[Self. View addsubview: imgview];

 

Lbltitle = [[uilabel alloc] initwithframe: cgrectmake (135,230,100, 30)];

Lbltitle. Text = @ "title ";

Lbltitle. textcolor = [uicolor redcolor];

[Self. View addsubview: lbltitle];

Uibutton * BTN = [[uibutton alloc] initwithframe: cgrectmake (100,300,100, 30)];

[BTN settitle: @ "add" forstate: uicontrolstatenormal];

[BTN addtarget: Self action: @ selector (ADD) forcontrolevents: uicontroleventtouchupinside];

[BTN setbackgroundcolor: [uicolor redcolor];

[Self. View addsubview: BTN];

}

-(Void) add {

_ Adds (imgview. Image, lbltitle. Text); // block the value

[Self. navigationcontroller poptorootviewcontrolleranimated: Yes];

}

-(Void) addcommunitywithblock :( addcommunity) addblock {

_ Adds = addblock;

}

Step 2:

Class B:

. M file:

[More addcommunitywithblock: ^ (uiimage * IMG, nsstring * Title ){

Nsdictionary * DIC = [nsdictionary dictionarywithobject: IMG forkey: title];

}];

Call block where you need to use the value. Description: More is an alias of Class. In this way, the value of page B is passed to the nsdictionary of page ....... I think it's clear! Hey ......

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.