A proxy is a design pattern used in iOS development. Today, I did a little practice as a proxy:
The following items enable switching between two pages and complete the transfer of values from one page to another when the page is toggled. It is easy to transfer values from the main page to other pages, but in turn it is difficult to transfer values from other pages to the home page, using the delegate design pattern.
Create a new iOS project, and then create a new Viewcontroller, named Twoviewcontroller, with a total of two viewcontroller, plus the viewcontroller generated by default when you create a new project. Where the new project is generated by default is the primary viewcontroller.
In order to use the proxy mode, we need to define a protocol, the Protocol is as follows:
1 #import<Foundation/Foundation.h>2 #import<UIKit/UIKit.h>3 //define a protocol that defines a method of passing a value in a protocol4 //define a protocol that defines a method of passing a value in a protocol5 //define a protocol that defines a method of passing a value in a protocol6 @protocolMyDelegate <NSObject>7 8 @required9-(void) ChangeValue: (NSString *) Stringnow;Ten One @end
The protocol defines a value-passing function that passes the value in the second Viewcontroller to the first Viewcontroller, and if it is not a proxy, we can only pass the value from the first Viewcontroller to the second Viewcontroller. And not in turn, from the second Viewcontroller to the first Viewcontroller pass the value (the specific reason did not understand, later understand and then fill up)
After completing the agreement, let the first Viewcontroller abide by this protocol, that is, in the first viewcontroller. h file, declare the first viewcontroller to abide by this Agreement <mydelegate>:
1 #import<UIKit/UIKit.h>2 3 //The first page applies the MyDelegate protocol4 //The first page applies the MyDelegate protocol5 //The first page applies the MyDelegate protocol6 #import "Mydelegate.h"7 @interfaceViewcontroller:uiviewcontroller <Mydelegate>8 9 Ten @end
Next, implement the ChangeValue method in the first Viewcontroller. m file:
1 #import "ViewController.h"2 #import "TwoViewController.h"3 @interfaceViewcontroller () {4Uitextfield *Textfieldnow;5 6 }7 8 @end9 Ten One A @implementationViewcontroller - - //The first page refers to the MyDelegate protocol, so implement the method defined in the. m file that must be implemented in MyDelegate the //The first page refers to the MyDelegate protocol, so implement the method defined in the. m file that must be implemented in MyDelegate - //The first page refers to the MyDelegate protocol, so implement the method defined in the. m file that must be implemented in MyDelegate --(void) ChangeValue: (NSString *) stringnow{ -textfieldnow.text=Stringnow; + } - + A at- (void) Viewdidload { - [Super Viewdidload]; - //Initialize Textfieldnow -Textfieldnow = [[Uitextfield alloc]initwithframe:cgrectmake ( -, $, $, -)]; - - //set the background color of Textfieldnow inTextfieldnow.backgroundcolor = [Uicolor colorwithred:0.139Green0.760Blue1.000Alpha1.000]; - to //add an event that makes the keyboard disappear after the input is completed for Textfieldnow + [Textfieldnow addtarget:self Action: @selector (Losefirstresponsder:) forControlEvents: Uicontroleventeditingdidendonexit]; - the //add Textfieldnow to view * [Self.view Addsubview:textfieldnow]; $ Panax Notoginseng - the } + A the //This function is TextField the virtual keyboard disappears after you press RETURN after you have entered +-(void) Losefirstresponsder: (ID) sender{ - [self resignfirstresponder]; $ } $ - --(Ibaction) Turntonext: (ID) Sender { theTwoviewcontroller *twoview =[[Twoviewcontroller alloc]init]; - Wuyi //The most critical part of the delegation mechanism, which points to the delegate object of the second interface before jumping to the second one the //The most critical part of the delegation mechanism, which points to the delegate object of the second interface before jumping to the second one - //The most critical part of the delegation mechanism, which points to the delegate object of the second interface before jumping to the second one WuTwoview.Delegate=Self ; - About $ //Pass the Textfieldnow value of the first interface to the namestring in the second interface -Twoview.namestring =Textfieldnow.text; - - A //jump to the second interface + [self Presentviewcontroller:twoview animated:yes completion:nil]; the - } $ the- (void) didreceivememorywarning { the [Super didreceivememorywarning]; the //Dispose of any resources the can be recreated. the } - in @end
The second Viewcontroller. h file is as follows:
1 #import<UIKit/UIKit.h>2 #import "Mydelegate.h"3 4 @interfaceTwoviewcontroller:uiviewcontroller5 6@property (nonatomic,retain) NSString *namestring;7 //define a variable delegate8 //define a variable delegate9 //define a variable delegateTen@property (Nonatomic,weak)ID<Mydelegate>Delegate; One A @end
The second Viewcontroller. m file is as follows:
1 #import "TwoViewController.h"2 3 @interfaceTwoviewcontroller () {4UILabel *label;5Uitextfield *TextFieldnow1;6 7 }8 9 @endTen One @implementationTwoviewcontroller A @synthesizenamestring; - @synthesize Delegate; -- (void) Viewdidload { the [Super Viewdidload]; - //Do any additional setup after loading the view from its nib. -label = [[UILabel alloc]initwithframe:cgrectmake ( -, -, -, -)]; - [Self.view Addsubview:label]; +Label.text =namestring; -TextFieldnow1 = [[Uitextfield alloc]initwithframe:cgrectmake ( -, $, $, -)]; + ATextfieldnow1.backgroundcolor = [Uicolor colorwithred:0.139Green0.760Blue1.000Alpha1.000]; at [TextFieldnow1 addtarget:self Action: @selector (Losefirstresponsder:) forControlEvents: Uicontroleventeditingdidendonexit]; - [Self.view Addsubview:textfieldnow1]; - - - } --(Ibaction) Backtohome: (ID) Sender { in [self dismissviewcontrolleranimated:yes completion:nil]; -[DelegateChangeValue:textFieldnow1.text]; to } + --(void) Losefirstresponsder: (ID) sender{ the [self resignfirstresponder]; * } $ Panax Notoginseng- (void) didreceivememorywarning { - [Super didreceivememorywarning]; the //Dispose of any resources the can be recreated. + } A @end
To enable the second page to pass a value to the first page by proxy, the key point is to have a pointer in the first page pointing to Self, which is twoview.delegate = self; this statement:
1 // The most critical part of the delegation mechanism, before jumping to the second interface, the second interface of the delegate object to its own 2// delegate mechanism the most critical part, Before jumping to the second interface, the delegate object of the second interface points to the most critical part of the 3// Delegate mechanism, pointing to the delegate object of the second interface before jumping to the second one. 4 Twoview. delegate = self;
The function of the agreement is to provide a function, which plays the role of the bridge.
iOS Development-ui Learning-delegate (proxy) usage