Description: This article is an application based on understanding block fundamentals, assuming that the reader already has the block basics.
Objective: To pass the value of Secondviewcontroller into Viewcontroller in block callback mode, and at some time, through block callback, can avoid delegate cumbersome
1, new single View Application project, new Secondviewcontroller
2, add Uilabel in Viewcontroller to display the value passed by Secondviewcontroller, set label to member variable, add a UIButton, Used to click Jump to Secondviewcontroller
@interfaceViewcontroller () {UILabel*_label;}@end- (void) viewdidload {[Super viewdidload]; //add UIButton to jump to SecondviewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Tosecondviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Graycolor]; [Btn addtarget:self Action: @selector (Tosecondviewcontroller) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; //add Uilabel to show values passed by Secondviewcontroller_label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _label.backgroundcolor=[Uicolor Yellowcolor]; [Self.view Addsubview:_label];}
3, add a Uitextfield to the SECONDVIEWCONTROLLER.M, set the member variable, enter the value, add a UIButton, click to jump back Viewcontroller
@interfaceSecondviewcontroller () {Uitextfield*_textfield;}@end- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; //Add a button to return ViewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Backtoviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Bluecolor]; [Btn addtarget:self Action: @selector (Backtoviewcontroller) forcontrolevents:uicontroleventtouchupinside]; [Self.view ADDSUBVIEW:BTN]; //Add a Uitextfield_textfield = [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _textfield.backgroundcolor=[Uicolor Greencolor]; [Self.view Addsubview:_textfield];}
4, in SecondViewController.h, use typedef to define a block and set it to a property, and declare a callback method
#import <UIKit/UIKit.h>// define a blockvoid (^returnvalueblock) (NSString * value); @interface Secondviewcontroller:uiviewcontroller@property (nonatomic,copy) returnvalueblock ReturnValueBlock; // callback Method -(void) returnvalue: (returnvalueblock) block; @end
5, implement the callback method, and call when you click the Return viewcontroller button
-(void) backtoviewcontroller { // when clicking Back to Viewcontroller, callback if ( Self.returnvalueblock! = nil) { self.returnvalueblock (_textfield.text); } [Self Dismissviewcontrolleranimated:yes completion:nil];} -(void) returnvalue: (returnvalueblock) block { = Block;}
6, use the Block method in VIEWCONTROLLER.M to assign a value to the label
-(void) tosecondviewcontroller { *second = [[Secondviewcontroller alloc] init]; [Second returnvalue:^ (NSString *value) { = value; }]; [Self Presentviewcontroller:second animated:yes completion:nil];}
Source Address: Https://github.com/rokistar/PassValueUsingBlock
IOS Block Callback