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 ......