iOS design mode-delegate mode

Source: Internet
Author: User

A delegate is a behavior that gives an object an opportunity to react to a change in another object, or a corresponding other object. The basic idea is to solve the problem together.

Delegate use of the occasion
    • Something has happened inside object A that wants to notify object B
    • Object B wants to listen to what's going on inside object A.
    • Object A wants to invoke a method of object B within its own method, and object A cannot have a coupling dependency on object B
    • Object A wants to pass data to object B

The result is the same: Object B is the proxy for object A (delegate)

Using in a program

1. Commission (a) the work to be done is:

1. Define proxy protocols, naming conventions for protocol names: control class name + Delegate2. Defining Proxy methods
    • Proxy methods are generally defined as @optional
    • Proxy method names begin with the control name
    • The proxy method has at least 1 parameters, passing the control itself out

3. Set proxy (delegate) object (e.g. myview.delegate = xxxx;)

    • Proxy Object Compliance Protocol
    • The implementation method of the Proxy object Implementation protocol

4. Invoke the Proxy method at the right time for the proxy object (delegate) to notify the agent what has happened

(Determines whether the agent implements the proxy method before calling)

2. Agent (B) the work to be done is:

    • Follow the agreement
    • Implementing a Delegate method

Example: Click "Load more Data" at the bottom of the interface view to notify the Controller to request data and refresh the table.

Delegate a related code

Defining proxy protocols, defining proxy methods

#import <UIKit/UIKit.h> @class tuangoufooterview;//Define a protocol, control class name + Delegate@protocol tuangoufooterviewdelegate <nsobject>//proxy methods are generally defined as @optional@optional//proxy method names beginning with the control name, the proxy method has at least 1 parameters, passing the control itself-(void) TUANGOUFOOTERDIDCLICKEDLOADBTN: (Tuangoufooterview *) Tuangoufooterview; @end @interface tuangoufooterview:uiview// Define the proxy. To use weak, avoid circular referencing @property (nonatomic,weak) id<tuangoufooterviewdelegate> delegate; @end

The proxy method that invokes the proxy object (delegate) at the appropriate time, notifies the agent

Notify the agent, first determine whether there is a way to implement the agent. if ([Self.delegate respondstoselector: @selector (tuangoufooterdidclickedloadbtn:)]) {     [self.delegate Tuangoufooterdidclickedloadbtn:self]; }
Agent (B) related code

Implementing a Proxy

@interface Viewcontroller () <TuangouFooterViewDelegate>

Set up Proxy

    Tuangoufooterview *footerview=[tuangoufooterview Footerview];    footerview.delegate=self;//setting the current Footerview as a proxy

Implementing Proxy methods

/** *  load more data */-(void) TUANGOUFOOTERDIDCLICKEDLOADBTN: (Tuangoufooterview *) tuangoufooterview{#warning Normal development: Send a network request to a remote server    //1. Add more model data    Tuangou *TG = [[Tuangou alloc] init];    Tg.icon = @ "ad_01";    Tg.title = @ "Newly added buy data:";    Tg.price = @ "+";    Tg.buycount = @ "0";    [Self.tgs ADDOBJECT:TG];        2. Refresh the table (tell TableView to reload the model data, call TableView's Reloaddata)    [Self.tableview reloaddata];}

iOS design mode-delegate mode

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.