Agents and protocols in iOS (delegate)

Source: Internet
Author: User

Source Address: http://download.csdn.net/download/swanzhu/9016861


I. Understanding of agreements and agents


Agreement

Format of the protocol:@protocol Keyword: statement of the agreement


For example

@protocol customalertviewdelegate <NSObject>

methods of <methods> protocols


@optional

The method declared under this keyword is an optional implementation.


@required

// This keyword declares a method that must implement the method, if not implemented, the compilation will report a warning and the program runs crashing.


// If the method is not specified as optional or must be implemented, the default is @required.


@end

Two agents


Agent is an abstract concept, if I want to do one thing, and I do not want to do, can let others help me to do, then others is my agent.


For example, I want to rent a house, I am more lazy, so I think the intermediary plus 50 dollars of fees can help me to find the house, I then to the intermediary call, commissioned intermediary to find a house, at this time intermediary is my agent, I commissioned him to help me find room.

In this case, the intermediary is the agent, the 50-dollar fee is the agreement.



1. first declare the protocol: Create the Protocol file

// Agreement statement

       @protocol changecolordelegate <NSObject>


Method

     -(void) ChangeColor;




(2) then (A) It is necessary to appoint who (principal) to execute the agreement.

Declare an attribute b*delegate in A.h ; Represents who the delegate (agent) is ( the object of Class B ).

id< Agreement > delegate; The principal can be any object that implements the Protocol class

// Statement

@protocol changecolordelegate;


Specifies the proxy object ,

@property (nonatomic,assign) id<changecolordelegate>delegate;

(3) Principal B(agent) is required to comply with the agreement and to complete the prescribed content in accordance with the agreement.

1.B First to implement the Protocol: in the B.h through the < Protocol name > Implementation protocol.

// implement the protocol, don't forget to import the header file at this time

@interface zyfirstviewcontroller:uiviewcontroller<changecolordelegate>

2. the agent needs to be set in B.M : a.delegate = self. (self refers to the object of class B)

Specify proxy

second.delegate=self;

3. Implement the Protocol method.

// Implement protocol method

-(void) changecolor{    self.view.backgroundcolor=[uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ( )%256/255.0 blue:arc4random ()%256/255.0 alpha:1.0];        [Self.navigationcontroller Popviewcontrolleranimated:yes];}



4. Specify under what circumstances the agent executes the protocol method.



// Specify under what conditions to execute


    [_delegate ChangeColor];








The basic idea of three-agent design mode: Two objects work together to solve the problem, through the agent to achieve the communication between object and object.

Essence: Invokes the method by passing the object.



Note: The protocol itself is not a class, there is no parent class and cannot define an instance variable, only the declaration of the method, not the implementation of the method;

Protocols are used to specifically declare methods implemented by other classes, and the methods are more flexible.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Agents and protocols in iOS (delegate)

Related Article

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.