I have just finished the OC proxy model today. I think it is a bit novel. I am the first to contact you.
A does one thing, but he cannot do it by himself. So he finds an agent B to complete it for him, and there is a protocol between them ), B inherits the Protocol to complete the tasks that a proxy gives to him.
Step: first write a protocol in Class A, declare some methods in the protocol, and declare a proxy attribute in the attribute. This attribute id <protocol> reminds me of jquery, I think it is like filtering out some classes. That is to say, not any class can act as a proxy of Class A. It must implement the Protocol and inherit from it (I do not know whether the words are appropriate or not ), some methods of the protocol, as to why, I understand it in this way. Because of other common methods in Class A, the methods in the protocol will be used, if this method is not implemented (except the @ option method), the method of Class A cannot be called. As for how to trigger the method, it must be implemented through action listening, whether it's scrollview or self-written, the start point, click, slide, and other events, as for some option events, there must be a judgment call in the trigger event, and you can choose to rewrite it later, class B wants to become a proxy for Class A, but it only assigns a good attribute to Class A that has been declared. Of course, this attribute has been filtered, that is, a protocol is implemented. Probably like this:
The code is attached:
1 class A 2 # import <uikit/uikit. h> 3 @ Class etundapp; 4 @ Class etundappview; 5 6 // declaration Protocol 7 @ protocol etundappviewdelegate <nsobject> 8 @ optional 9-(void) appviewclickeddownloadbutton :( etundappview *) appview; 10 @ end11 12 @ interface etundappview: uiview13/** 14 * proxy 15 */16 @ property (nonatomic, strong) ID <etundappviewdelegate> delegate; 17 @ property (nonatomic, strong) etundapp * app; 18 + (instancetype) appview;
Class A implementation and event triggering
-(Ibaction) download :( uibutton *) sender {
// Change the status
Sender. Enabled = no;
// Notification proxy
If ([self. Delegate respondstoselector: @ selector (appviewclickeddownloadbutton :)]) {
[Self. Delegate appviewclickeddownloadbutton: Self];
}
}
19 @ end20 Class B 21 // declare proxy 22 appview. Delegate = self; 23 rewrite protocol Method 24-(void) appviewclickeddownloadbutton :( etundappview *) appview {}
OC learning path-proxy Mode