Block is a block of code,
Block definition
return value (^ block name) (parameter 1, parameter 2 ...) );
You can use TypeDef to rename a block when you define it.
typedef void(^blockname) (nsstring *string);
Block and function similarity:
(1) Can save code
(2) There is a return value
(3) Tangible parameters
(4) Call the same way.
Specific implementation process:
//SecondViewController.h#import<UIKit/UIKit.h>typedefvoid(^blockname) (NSString *string);@interfaceSecondviewcontroller:uiviewcontroller@property (nonatomic, copy) Blockname Sendblock;@end//SECONDVIEWCONTROLLER.M-(Ibaction) Actiontow: (ID) Sender {Self.sendblock (self.textfile.text); [Self Dismissviewcontrolleranimated:yes completion:nil];}//VIEWCONTROLLER.M#import "ViewController.h"#import "SecondViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*lable, @property (Strong, nonatomic) Secondviewcontroller*SECONDVC;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.SELF.SECONDVC =[[Secondviewcontroller alloc]init];//How to resolve circular references in arc case: _weak__weak Viewcontroller *weakthis =Self ; Self.secondVC.sendBlock= ^ (NSString *string){//if you want to access the property, you also use __strong to decorate it. __strong Viewcontroller *strongthis =Weakthis; StrongThis.lable.text=string; };}-(Ibaction) Actionone: (IDsender {[self PresentViewController:self.secondVC animated:yes completion:nil];}
This article GitHub address Https://github.com/zhangkiwi/iOS_SN_Block
Block of ios-design mode