iOS Agent/protocol @protocal

Source: Internet
Author: User
<span id="Label3"></p><p><p>Understand:</p></p><p><p>@protocal can define a Protocol. If a class uses this protocol, it needs to follow this Protocol. The most common is uitableviewdelegate, uitableviewdatasource this two guy. Our class can use these two protocols and implement the method of protocol definition (numberofrowsinsection, Heightforrowatindexpath ...) and then need to bind. Because our class has implemented both of these Protocols. So the TableView in our class can be bound like this:</p></p><p><p>Tableview.delegate = self;</p></p><p><p>Tableview.datasource = self;</p></p><p><p></p></p><p><p>Then there is the question, why write the above 2 words, our TableView can callback our class to implement the function of the Protocol.</p></p><p><p>You can imagine how UITableView callback our defined protocol Functions: (presumably:)</p></p><p><p>[_delegate heightforrowatindexpath:indexpath];</p></p><p><p>[_datasource numberofrowinsection:section];</p></p><p><p>Note: The above _delegate,_datasource is the encapsulated attribute (object) in the UITableView represented.</p></p><p><p></p></p><p><p>If you write an object-oriented, protocol in ios, an interface in java, you should be able to understand the code Above.</p></p><p><p>It is written so that callbacks can be implemented. Doubt Resolved.</p></p><p><p></p></p><p><p>In order to better illustrate the meaning of @protocal. Look at a small example that I Wrote.</p></p><p><p>Use one of the simplest delegate to tell the Delegate:</p></p><p><p>The first is Viewcontroller's definition of delegate, which is Realized.</p></p><p><p>Here is the code for the Viewcontrolle class:</p></p><pre><pre>1 #import <UIKit/UIKit.h> 2 3//declaration Agreement passvaluedelegate 4 @protocol passvaluedelegate <NSObject> 5 6- (void) mypassvalue: (nsstring*<span>) msg; 7-(void<span>) mycallback; 8 9 @end10 Each//viewcontroller class uses the protocol defined above and implements two methods in the protocol, @interface viewcontroller: uiviewcontroller<passvaluedelegate><span>{ <span>}15//viewcontroller own a passvaluedelegate protocol object 17/ /similar to the UITableView class has retained two Delegate/datasource protocol objects @property nsobject<passvaluedelegate> *<span>mdelegate;19 20-( void<span>) callmetodo;21 @end</span> </span> </span> </span> </span> </span></pre></pre><pre>1 #import "ViewController.h" 2 #import "BViewController.h" 3 4 @interface<span>Viewcontroller () 5 6 @end 7 8 @implementation<span>Viewcontroller 9-(void<span>) Viewdidload {11<span>[super viewdidload];12 13//write A button, click Jump to Bviewcontroller on UIButton *button = [[UIButton Alloc]initwithframe:cgrec Tmake (100, 100, 80, 50<span>)];15 Button.layer.backgroundColor =<span>[uicolor greencolor]. Cgcolor;16<span>[button addtarget:self action: @selector (buttonpressed:) forcontrolevents:uicontroleventtouchupinside];17<span>[self.view addsubview:button];18<span><span>}19 20//here is how the code will invoke the implementation of the _mdelegate Object. You can look at the following Code. -(void <span>) callmetodo{22 [_mdelegate mypassvalue:@ "cool" <span>];23 <span> [_mdelegate mycallback];24 <span>}25 26 -(void) buttonpressed: (id <span>) sender{27 bviewcontroller *BVC = <span> [[bviewcontroller alloc]init];28// Viewcontroller himself implemented this protocol, 29//then BVC can refer to Viewcontroller object, callback Viewcontrller implementation of the two METHODS. Bvc.mydelegate = <span> self;31 <span> [self presentviewcontroller:bvc animated:yes completion:nil];32 <span>}33-//V Iewcontrller uses the protocol and enforces 2 methods (void) mypassvalue: (nsstring* <span>) msg{36 NSLog (@ "%@" <span>, msg), PNS <span>}38 39-( void <span>) mycallback{40 NSLog (@ "callback Call now ..." <span>) (}42 <span>)-(void <span>) didreceivememorywarning {$ <span> [super Didreceivememorywarning];45//Dispose of Any resources this can be recreated.46 <span>}47 @end </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p>Here is the code for the Bviewcontrller class:</p></p><pre><pre>1 #import <uikit/uikit.h>2 #import "ViewController.h" 3 4 @interface<span> bviewcontroller:uiviewcontroller5 6//maintain One An object using the Passvaluedelegate protocol 7 @property nsobject<passvaluedelegate> *<span>mydelegate;8 9 @end</span> </span></pre></pre><pre><pre>1 #import "BViewController.h" 2 3 @interface<span> bviewcontroller () 4 5 @end 6 7 @implementation<span> Bviewcon Troller 8 9-(void<span><span> [super viewdidload];11 <span>}13-(ibaction) buttonpressed: (id<span><span> [_mydelegate mycallback];19 [_mydelegate mypassvalue:@ "Message from b_view_controller"<span><span>}22-(void<span><span><span>}27 @end</span></span></span> </span></span></span></span></span></span></span></span></span></pre></pre><p><p>The benefit of the Protocol is to standardize the unified interface. If you use a protocol, you must write the code according to the function interface defined by the Protocol.</p></p><p><p>Here is Cviewcontroller used the protocol defined in viewcontroller, and then cviewcontroller to follow the terms of the agreement,</p></p><p><p>Implement the two methods in the Protocol. The code for Cviewcontroller is as Follows:</p></p><pre><pre>1 #import <uikit/uikit.h>2 #import "ViewController.h" 3 4//use The protocol passValueDelegate5 in Viewcontroller @interface Cviewcontroller:uiviewcontroller<passvaluedelegate>6 7 @end</pre></pre><pre><pre>1 #import "CViewController.h" 2 3 @interface<span> cviewcontroller () 4 5 @end 6 7 @implementation<span> Cviewcon Troller 8 9-(void<span><span> [super viewdidload];11 <span>}13-(void) mypassvalue: (nsstring *<span>) msg{15 NSLog (@ "c:%@"<span><span>}17-(void<span>) mycallback{19 NSLog (@ "C Callback.") <span><span>}21-(void<span><span><span>}26 @end</span></span></span> </span></span> </span> </span></span> </span> </span> </span></span> </span> </span></pre></pre><p><p>Give me another Chestnut. In fact, similar to Viewcontroller and Bviewcontroller Code.</p></p><p><p>This is the Dviewcontroller implementation of the Passvaluedelegate Protocol. And let Viewcontroller's mdelegate to keep Dviewcontroller objects,</p></p><p><p>Then Viewcontroller calls the method, you can callback the method in the Viewcontroller in the Dviewcontroller.</p></p><p><p>(the above pattern is not similar to Uitableview's use Process.) In the current Viewcontroller to bind the TableView proxy,</p></p><p><p>You can then invoke the proxy implementation method for the current Viewcontroller in TABLEVIEW. A model with a similar question to the one at the Beginning)</p></p><p><p>The code is very similar. The following is the code for Dviewcontroller:</p></p><pre><pre>1 #import <uikit/uikit.h>2 #import "ViewController.h" 3 4//using passvaluedelegate Protocol 5 @interface Dviewcontroller: Uiviewcontroller<passvaluedelegate>6 7 @end</pre></pre><pre>1 #import "DViewController.h" 2 3 @interface<span>Dviewcontroller () 4 5 @end 6 7 @implementation<span>Dviewcontroller 8 9-(void<span><span>) viewdidload {ten <span> [super viewdidload];11//do Any additional setup after loading the view from its NIB . <span>}13 14//if The protocol is used, it is necessary to implement the Protocol Method. -(void <span>) mycallback{16 NSLog (@ "this is D vc" <span>); <span>}18-(void) mypassvalue: (nsstring * <span>) msg{20 NSLog (@ "i am D viewcontroller" <span>) <span>}22 (ibaction) buttonpress: (id <span>) sender {25//click button, Create Viewcontroller (can be understood as creating a UITableView Object) viewcontroller* VC = <span> [[viewcontroller Alloc]init];27//bind Proxy 28 Vc.mdelegate = <span> self;29//active callback. (can be understood as TableView in the reload function, active reload) the <span> [vc callmetodo];31//summarize the above code: TableView can not be proactive callback, 32//because TableView is a control, The control is a listener who can initiate callbacks to the protocol function based on the click Interaction. 33//above is for example, so an active callback is Used. <span>}35-(void <span>) didreceivememorywarning {notoginseng <span> [super didreceivememorywarning];38//Dispose of any Reso Urces that can recreated.39 <span>}40 @end </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></pre><p><p>The above code, already described the use of protocal, as well as the Principle.</p></p><p><p>Summarize:</p></p><p><p>1. Specific use: in the uniform canonical function interface. All classes require functionality that can be used to standardize unity using protocal.</p></p><p><p>For example: people, cows, horses are animals, they all have a few common characteristics: run, eat, drink.</p></p><p><p>This is the function that animals have, and you can use protocal to standardize the unified interface.</p></p><p><p>2. function callback, Proxy. Similar to block, but there is a lot of difference with BLOCK. After requesting the network, fetch the data to the server and callback to the initial interface.</p></p><p><p>At this point, you can have the initial interface implement the protocol, and then let the request server class maintain an initial interface of the protocol object, and then when the data is obtained,</p></p><p><p>The server can implement protocol specific methods based on this protocol object, which callback the initial Interface.</p></p><p><p>Protocal also has 2 keywords, required and optional KEYWORDS.</p></p><p><p>If you do not write these two keywords when you define the function interface of the protocol, the default is Required.</p></p><p><p>Required means that whoever uses this protocol will have to implement the method in this protocol, which is mandatory.</p></p><p><p>Optional means that you can implement the method within the protocol, or you can not implement the protocol inside the method, is Optional.</p></p><p><p>As for protocal the specific agent, the Agreement.</p></p><p><p>The agent is to give things to others to do. I just have to keep that Person's phone number.</p></p><p><p>The proxy object that holds a B in Class A refers to the use of proxy p in p,b, and the method of implementing the proxy is Dosomething.</p></p><p><p>Class A is bound to Class B and requires only A.P = b; What happens later, like:</p></p><p><p>A class one day wants B to do something, as long as this comes [P dosomething]. B is going to do it well.</p></p><p><p>The agreement is a norm, and we must all do it according to the Agreement.</p></p><p><p>White is the same as the Java interface functions, abstraction into high-level, shielding Differences. It is then invoked by the object reference to which it is HELD.</p></p><p><p>Good performance of the Object's Characteristics.</p></p><p><p>iOS Agent/protocol @protocal</p></p></span>

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.