Delegate (delegate) value transfer-as the name suggests, it is to entrust others to handle affairs, that is, when one thing happens, it is not handled by itself, so that others can handle it.
// Pass the value by proxy
Note: Upload from the back to the front (if it is uploaded from the back to the back, it will fail to be uploaded)
// Process:
1. The next interface defines a protocol and defines an attribute named delegate.
2. The moment when the previous interface enters the next interface (that is, after an interface is created), let the previous interface act as the delegate of the next interface
3. Implement proxy methods on the previous interface
4. The next interface is suitable for the proxy, execution, and proxy method (the passed value is included in the proxy method as a parameter)
The Code is as follows:
# Import "firstviewcontroller. H "# import" secondviewcontroller. H "# import" uibutton + create. H "@ interface firstviewcontroller () {uilabel * _ label;} @ end @ implementation firstviewcontroller-(void) dealloc {[_ label release]; [Super dealloc];} -(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) attributes {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// initim initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor redcolor]; self. navigationitem. title = @ "Homepage"; self. view. userinteractionenabled = yes;/*** 1. create a uibutton, * 2. add a response event to jump from the home page to the second page. */uibutton * button = [uibutton systembuttonwithframe: cgrectmake (100,120, 50, 50) Title: @ "push" target: Self action: @ selector (didclickbuttonaction)]; button. userinteractionenabled = yes; [self. view addsubview: button];/*** 1. create a uilabel * 2 on the 1st interfaces. upload the string entered in the input box on the second page through the proxy method * 3. then assign a value to uilabel */_ label = [[uilabel alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ label. backgroundcolor = [uicolor greencolor]; [self. view addsubview: _ label]; // do any additional setup after loading the view .} -(void) didclickbuttonaction {/*** 1. use the push method to launch the next page * 2. upload the string entered in the input box on the second page through the proxy method * 3. in this way, the strings entered in the homepage input box are uploaded to the uilabel on the second page. */secondviewcontroller * secondvc = [[secondviewcontroller alloc] init]; secondvc. delegate = self; // confirm the proxy [self. navigationcontroller pushviewcontroller: secondvc animated: Yes]; [secondvc release];} // implement proxy method-(void) passbyvaluesecondvc :( secondviewcontroller *) secondvc text :( nsstring *) text {_ label. TEXT = text;}-(void) didreceivemorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
#import <UIKit/UIKit.h>@class SecondViewController;@protocol secondViewControllerDelegate <NSObject>- (void)passByValueSecondVC:(SecondViewController *)secondVC text:(NSString *)text;@end@interface SecondViewController : UIViewController@property (nonatomic,assign)id<secondViewControllerDelegate>delegate;@end
# Import "secondviewcontroller. H "# import" uibutton + create. H "# import" firstviewcontroller. H "@ interface secondviewcontroller () {uitextfield * _ textfield; // create an input box} @ end @ implementation secondviewcontroller-(void) dealloc {[_ textfield release]; [Super dealloc];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: callback]; If (Self) {// initim initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor orangecolor]; self. navigationitem. title = @ "second page";/*** 1. create a uibutton, * 2. add a response event and return it to the home page from the second page. */uibutton * button = [uibutton systembuttonwithframe: cgrectmake (100,120, 50, 50) Title: @ "back" target: Self action: @ selector (didclickbuttonaction)]; [self. view addsubview: button];/*** 1. create an input box **/_ textfield = [[uitextfield alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ textfield. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: _ textfield]; // do any additional setup after loading the view .} -(void) didclickbuttonaction {// call the proxy method, _ textfield. text: [_ delegate passbyvaluesecondvc: Self text: _ textfield. text]; [self. navigationcontroller poptorootviewcontrolleranimated: Yes];}-(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Pass value by proxy