Proxy and Protocol (delegate) in ios)

Source: Internet
Author: User

Proxy and Protocol (delegate) in ios)

 

 

1. Understand protocols and Proxies

 

Protocol

 

Protocol format: @ protocol Keyword: protocol Declaration

 

For example

@protocol CustomAlertViewDelegate 
 

Protocol Method

 

@ Optional

// The method declared under this keyword is an optional method.

 

@ Required

// The method declared by this keyword is a required method. If this method is not implemented, a warning will be reported during compilation and the program running will crash.

 

// If no method is specified, it is optional or required. The default value is @ required.

 

@ End

Proxy 2

 

Proxy is an abstract concept. If I want to do something, but I don't want to do it myself, I can ask someone to help me, then others will be my proxy.

 

For example, if I want to rent a house, I am relatively lazy, so I think that the intermediary can help me find the house by adding 50 yuan of service fees, so I called the intermediary, entrust an intermediary to find a house. At this time, the intermediary is my proxy. I entrust him to help me find a house.

In this example, the intermediary is the proxy, and the handling fee of 50 RMB is the agreement.

 

 

 

1. Declare the Protocol first: Create a Protocol File

// Protocol statement

 

       @protocol ChangeColorDelegate 
 


 

// Method

 

     -(void)changeColor;


 

 

 

(2) then (A) specifies who (principal) is allowed to execute the protocol.

In A. h, declare A property B * delegate, which represents the principal (proxy) who is the object of Class B ).

Id <protocol> delegate; the principal can be any object that implements the Protocol class.

// Statement

 

@protocol ChangeColorDelegate;

 

 

// Specify the proxy object ,,

 

 @property(nonatomic,assign)id
 
  delegate;
 

 

(3) The principal B (agent) must abide by the agreement and complete the provisions in accordance with the Agreement.

1. B First implements the Protocol: in B. h, the Protocol is implemented through the <protocol name>.

// Implement the Protocol. Do not forget to import the header file.

 

@interface ZYFirstViewController : UIViewController
 

 

2. You need to set proxy in B. m: a. delegate = self. (self refers to the object of Class B)

// Specify the proxy

 

second.delegate=self;

 

 

3. implement the Protocol.

// Implementation 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 the circumstances under which the proxy executes the Protocol method.

 

 

// Specify the conditions for execution

 

 

    [_delegate changeColor];


 

 

 

 

 

 

 

 

The basic idea of the Three-agent design mode: two objects work together to solve the problem and implement communication between objects through proxies.

Essence: Call methods by passing objects.

 

 

Note: The Protocol itself is not a class. There is no parent class and instance variables cannot be defined. It only declares the method and does not implement the method;

The protocol is used to specifically declare methods implemented by other classes, and the method functions are more flexible.


 

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.