Block code blocks, also known as closures and C-language functions, are similar to functions with reverse values and callbacks.
The block formula is divided into two types:
① Declaration and implementation write to a piece of the formula
return value type (^block name) (parameter list argument type parameter name) = ^ (parameter list argument type parameter name) {
Implementation code (value of return value type if return value is required)
};
Call: Block name (parameter);
② Declaration and implementation of separate write formulas
(1) statement
return value type (^block name) (parameter list);
(2) Realize
Block name = ^ (parameter list) {
Implementation code (value of return value type if return value is required)
};
(3) Call
Block name (actual argument);
Note? There must be an implementation method before the call
Today, the main direction of the block is to pass the value (direction value is to give him a value at the time of the callback), through the button's example to tell how the block reverse value.
The specific code is as follows:
Create a window with a navigation bar in APPDELEGATE.M
#import "AppDelegate.h"#import "ViewController.h" @interfaceappdelegate ()@end @implementationappdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Self.window=[[UIWindow Alloc]initwithframe:[uiscreen mainscreen].bounds]; Self.window.rootViewController=[[Uinavigationcontroller Alloc]initwithrootviewcontroller:[[viewcontroller Alloc]init]]; [Self.window makekeyandvisible]; returnYES;} @end
Create two buttons inside the viewcontroller.m file, and create an inheritance from Uiviewcontroller named Blockviewcontroller class, import viewcontroller.m file inside
#import "ViewController.h"#import "BlockViewController.h" @interfaceViewcontroller () {UIButton*Testbutton;}@end @implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; [Self Createbutton]; } - (void) createbutton{//button to put the pictureTestbutton=[UIButton Buttonwithtype:uibuttontypecustom]; Testbutton.frame= CGRectMake ( Max, -, -, -); Testbutton.backgroundcolor=[Uicolor Browncolor]; TestButton.layer.cornerRadius= -; TestButton.layer.masksToBounds=YES; [Self.view Addsubview:testbutton]; //buttons for clickingUIButton*button =[UIButton Buttonwithtype:uibuttontypecustom]; Button.frame= CGRectMake ( -, -, $, -); [Button Settitle:@"go to the next page"Forstate:uicontrolstatenormal]; Button.backgroundcolor=[Uicolor Browncolor]; Button.layer.cornerRadius=6; [Button addtarget:self action: @selector (Clickbutton) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button]; } //trigger events for buttons- (void) clickbutton{Blockviewcontroller*block =[[Blockviewcontroller alloc]init]; //the implementation of blockBlock.buttonblock= ^ (UIImage *image) {[Testbutton setimage:image forstate:uicontrolstatenormal]; }; [Self Presentviewcontroller:block animated:yes completion:nil]; } @end
Declare the Block method in the BlockViewController.h file you created and use the block as a property
#import <UIKit/UIKit.h>@interface blockviewcontroller:uiviewcontroller// Block declaration This block is treated as a property @property (nonatomic,copy)void(^buttonblock) (UIImage * image); @end
Implementing block callbacks in the blockviewcontroller.m file
#import "BlockViewController.h"@interfaceBlockviewcontroller () {UIButton*Testbutton;}@end @implementationBlockviewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; [Self Backbutton];}- (void) backbutton{Testbutton=[UIButton Buttonwithtype:uibuttontypecustom]; Testbutton.frame= CGRectMake ( Max, -, -, -); [Testbutton setimage:[uiimage imagenamed:@"Avatar 3.jpg"] Forstate:uicontrolstatenormal]; Testbutton.backgroundcolor=[Uicolor Browncolor]; TestButton.layer.cornerRadius= -; [Self.view Addsubview:testbutton]; UIButton*button =[UIButton Buttonwithtype:uibuttontypecustom]; Button.frame= CGRectMake ( -, -, $, -); [Button Settitle:@"back to previous page"Forstate:uicontrolstatenormal]; Button.backgroundcolor=[Uicolor Browncolor]; Button.layer.cornerRadius=6; [Button addtarget:self action: @selector (Clickbutton) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button];}- (void) clickbutton{//Block's callbackSelf.buttonblock ([UIImage imagenamed:@"Avatar 3.jpg"]); //Go back to previous page[self Dismissviewcontrolleranimated:yes completion:nil];}@end
Block code blocks reverse-pass values