The Rootviewcontroller code is as follows:
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #define kdebugprint nslog (@ "%s", __func__) @interface RootViewController () { uilabel *_label;} @end @implementation rootviewcontroller/* forward value Create first interface jump to second interface via first interface If the second interface is oriented by the first boundary pass value forward value Property pass value second to first interface value Reverse value downlevel interface up to one level interface value--- "Reverse Value Reverse value: 1. Proxy value Downlevel interface to the TextField of the content to the upper level, then the subordinate interface can be delegated to the upper interface Modify label Value Second interface (active side) can develop a protocol canonical proxy behavior, first interface (passive side) Compliance protocol as proxy 2. Single case 1. System Singleton 2. Custom singleton 3. Notification pass-through value 4.nsuserdefaults 5.block Transfer value */- (void) viewdidload { [super Viewdidload]; [self showui];} - (void) Showui { self.view.backgroundcolor = [uicolor graycolor]; _label = [mycontrol creatlabelwithframe:cgrectmake (0, 30, 300, 30) text:@ "XXX"]; _label.backgroundcolor = [uicolor yellowcolor]; [ self.view addsubview:_label]; uibutton *button = [mycontrol creatbuttonwithframe:cgrectmake (10, 200, 300, 50) target:self SEL: @selector (btnclick:) tag:201 image:nil title:@ "switch to second"]; [self.view addsubview:button];} - (void) Btnclick: (uibutton *) btn { //Create a new second object each time you click the button SecondViewController *svc = [[SecondViewController alloc] init]; [svc setmyblock:^ (NSSTRING *TEXTSTR) { _label.text=textStr; }]; [self presentViewController:svc animated:YES completion:nil]; [svc release]; }- (void) didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated.} @end
Secondviewcontroller Point h file declares the following code:
#import <uikit/uikit.h>//-----------typedef void (^changetextblock) (NSString *textstr); @interface secondviewcontroller:uiviewcontroller{//void (^myblock) (NSString *textstr); Changetextblock _myblock;} Modify and get-(void) Setmyblock: (Changetextblock) block;-(changetextblock) myblock; @end
#import "SecondViewController.h" #import "MyControl.h" #define kdebugprint nslog (@ "%s", __ func__) @interface SecondViewController () { uitextfield *_textfield;} @end @implementation secondviewcontroller- (void) dealloc { kdebugprint; [_myblock release]; [super dealloc];} -(void) Setmyblock: (Changetextblock) block{ if (_myblock!=block) { [_myblock release]; _ myblock=[block copy];//copy block }}-(changetextblock) myBlock{ return _myblock;} - (void) viewdidload { [super viewdidload]; Self.view.backgroundcolor = [uicolor yellowcolor]; [self showui];} - (void) shOwui { uibutton *button = [mycontrol creatbuttonwithframe: CGRectMake (10, 30, 300, 30) target:self sel: @selector (btnclick:) tag:301 image:nil title:@ "Back"]; [self.view addsubview:button]; uibutton *button2 = [mycontrol creatbuttonwithframe: CGRectMake (10,200 , 300, 30) target:self sel: @selector (btnClick2:) tag:302 image:nil title:@ "Pass Value"]; [self.view addsubview:button2]; _textField = [MyControl Creattextfieldwithframe:cgrectmake (10, 100, 300, 30) placeholder:nil delegate:nil tag:100]; [self.view addsubview:_textfield];} Keyboard- (void) Touchesbegan: (nsset *) Touches withevent: (uievent *) EVent { [_textfield resignfirstresponder];} - (void) Btnclick: (uibutton *) btn { //return to the previous level [self dismissviewcontrolleranimated:yes completion:nil];} - (void) BtnClick2: (uibutton *) btn { //Click Pass Value To delegate block execution modify the value of the first interface label if (self.myblock) { //_myblock (_textfield.text); //equivalent to the previous sentence self.myblock (_textfield.text); }else{ nslog (@ "no incoming block"); }} @end
iOS Reverse Value--block method