Proxy mode in the iOS language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.