What is an agent and why does it need to be represented?
When a class or a controller needs to complete some "action", it needs another class or view controller as the carrier of its action.
A single example in practical application
For example, when the left-hand slider Leftviewcontroller has a user login to display the avatar, click on the avatar will go to the user Settings page. The user Settings page actually needs to be entered from Rootviewcontroller push. So when the user clicks on the left slider Leftviewcontroller on the user picture, the left side of the slider to the agent Rootviewcontroller to perform the creation of the user Settings page and bounce into the series of actions. This is the implementation of an agent.
In this process, the Avatar button is the Click event belonging to the left slide bar Leftviewcontroller, but the true execution after the click is required Rootviewcontroller to complete. Then the role of Leftviewcontroller is to tell the system when the user clicks on the Avatar button, please let my agent to perform the action after clicking the button.
Using code to implement a proxy
The first step is to make the following declaration in LeftViewController.h, @required indicates that its agent must execute its next methods @optional indicate that its agent can selectively execute the method below it.
@protocol Leftmenuviewcontrollerdelegate <NSObject>@required-(void) Userimagetap: (Uiimageview * Sender; @optional-(void) loginbuttonclicked: (UIButton *) sender; @end @interface IDdelegate; @end
Then you need to implement the Proxy method in LEFTVIEWCONTROLLER.M
-(void) Userimagetap: (ID) sender{ if ([_delegate respondstoselector:@ Selector (USERIMAGETAP:)]) { [_delegate userimagetap:sender];} }
Finally, you need to declare leftviewcontroller.delegate = self in rootviewcontroller to perform its specific proxy method operation, and finally execute the proxy method that needs to be executed.
_LEFTMENUVC. delegate = self ; -(void) Userimagetap: (Uiimageview *) sender{ [self pagereclaim]; *ACCOUNTVC = [[Accountsettingviewcontroller alloc]initwithnibname:@ 'accountsettingviewcontroller [ bundle:nil]; [Self.navigationcontroller PUSHVIEWCONTROLLER:ACCOUNTVC Animated:yes]; [ACCOUNTVC release]; [Self leftnavigationbuttonclicked];}
Proxy mode in the iOS language