What is agreement agency? Proxy is a universal design pattern that is well supported in iOS, with specific syntax to support it.

There are three main parts: 1. Agreement: used to stipulate what should be done and what must be done. 2. Agent: According to the formulated agreement, complete the functions required by the principal. 3. Principal: According to the formulated agreement, propose the functions that the agent needs to implement. \

What scenarios are used? Here’s an example: Such as I need to buy a only the United States have to sell things, and people in China, I didn’t also the way to buy things to the United States, this time I will need to go online to find the United States bought little sister to help me buy this stuff, and the structure of the deal is likely to be through the taobao shop ma father, so when the rules of taobao is the our agreement, and I am the principal, American daigou little sister is the agent, THE money I give daigou little sister is the parameter I pass in, and the last thing he sends me is the return result.

How to write the code? 1. Make an agreement

@protocol MPAmericanBuyDelegate <NSObject>
- (void)buySomethingFromAmericanWithPrice:(CGFloat)aPrice;
@end
Copy the code

2. Trigger agreement conditions (proxy)

// declare @interface MPAmericanBuyView: UIView @property (nonatomic,weak) id<MPAmericanBuyDelegate> delegate; @end // implement - (void)finishedBuyBtnClicked {// _delegate &&[_delegate respondsToSelector:@selector(buySomethingFromAmericanWithPrice:)]) { [_delegate buySomethingFromAmericanWithPrice:[self.priceField.text floatValue]]; }}Copy the code

3. Implementation protocol method (delegate)

- (void) buySomethingFromAmericanWithPrice: CGFloat aPrice {NSLog (@ "finished"); }Copy the code