iOS design mode-delegate (delegate)

Source: Internet
Author: User
<span id="Label3"></p><p><p>A delegate (delegate) is also called an agent, which is a common design pattern in iOS Development. We can easily implement this design pattern with the help of protocol (see blog: OBJECTIVE-C protocol (protocol)).</p></p>What is an agent?<p><p>Apple's official documentation gives a clear explanation:</p></p><p><p> <span style="color: #333333; line-height:24px; font-family:georgia, ' bitstream Charter ', serif;"> <em> delegation is A-simple and Powerful-pattern in which one object-a program-acts on behalf of, or-coordination with, another obj Ect. The delegating object keeps a reference to the other object-the Delegate-and at the appropriate time sends a message to it . The message informs the delegate of an event, the delegating object is about to handle or have just Handled. The delegate respond to the message by updating the appearance or state of itself or other objects in the application, And in some cases it can return a value this affects how an impending event is Handled. The main value of delegation is the IT allows-easily customize the behavior of several objects in one central Obje Ct. </em> </span> </p></p><p><p>It is a simple and powerful design pattern for an object to "represent" another object and interact with other objects in the Program. The main object (in this case, delegating Object) maintains a reference to a proxy (delegate) and sends a message to the agent at the appropriate Time. This message informs the "agent" that the main object is about to be processed or that an event has been processed. The agent can respond to the message of the event sent by the main object by updating the UI interface or other state of the Object. or, in some cases, return a value to affect how other upcoming events should be Handled. The main value of the agent is that it allows you to easily customize the behavior of various Objects. Note that the agent here is a noun, which is itself an object that is specifically representative of the object being proxied to deal with other objects in the Program.</p></p><p><p></p></p><strong><strong>cocoa agents in the</strong></strong><p><p>The Cocoa touch framework is heavily used in proxy design patterns, where each UI control class declares a type ID of delegate or datasource, and looking at Cocoa's header file can find many of the following properties:</p></p><p><p></p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1">@property<span class="s1">(nonatomic<span class="s1">, assign<span class="s1">) ID<span class="s1"><<span class="s2">uiactionsheetdelegate<span class="s1">> delegate; <span class="s3">//weak Reference</span> </span> </span> </span> </span> </span> </span></p></p><p class="p1"><p class="p1"><span class="s1">The usual format is @property<span class="s1">(nonatomic<span class="s1">, assign<span class="s1">) ID<span class="s1"><protocol_name<span class="s1">> delegate; That is, the agent should follow a certain protocol, that is, only the class object that follows the agreement is eligible for PROXY. This also requires that the proxy class must declare the @required method in the header file to follow this Protocol_name protocol and implement it, @optional method is Optional. </span></span></span></span></span></span></p></p><p class="p1"><p class="p1">Take Uiactionsheet as an example, we define a view that triggers uiactionsheet when a button in the view is clicked, when the user completes an action on uiactionsheet, such as the destruct button being pressed, Or the Cancel button is pressed, Uiactionsheet sends a message to delegate, and the delegate completes the response to the User's action, such as printing a string to the Screen. The diagram illustrates the following:</p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1">first, we create a tab-based project that adds code to the FirstViewController.h, which follows the Uiactionsheetdelegate protocol:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=1&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">@interface Firstviewcontroller:uiviewcontroller <UIActionSheetDelegate></li> </ol> </ol><p><p></p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1">Add a button to the view to trigger the actionsheet, and then write the response code for the Button:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=2&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) invokeactionsheet: (id) Sender {</li></li> <li><li></li></li> <li class="alt"><li class="alt">Uiactionsheet *actionsheet = [[uiactionsheet alloc]</li></li> <li><li>initwithtitle:@<span class="string">"Delegate Example"</span></li></li> <li class="alt"><li class="alt">Delegate:self <span class="comment">//telling This class (viewcontroller) to implement Uiactionsheetdelegate</span></li></li> <li><li>cancelbuttontitle:@<span class="string">"Cancel"</span></li></li> <li class="alt"><li class="alt">destructivebuttontitle:@<span class="string">"destruct"</span></li></li> <li><li>otherbuttontitles:@<span class="string">"button 1" @<span class="string">"button 2", nil]; </span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li>[actionsheet showInView:self.tabBarController.view];</li></li> <li class="alt"><li class="alt">[actionsheet release];</li></li> <li><li>}</li></li> </ol><p><p><br>Note that one of the important settings above is that there is a delegate:self in the parameter, which indicates that the Uiactionsheet agent is self, or firstviewcontroller.</p></p><p><p></p></p><p><p><br>Then implement the methods in the Uiactionsheetdelegate in Firstviewcontroller.m:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=3&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><span class="preprocessor">#pragma Mark--uiactionsheet delegate Methods</span></li></li> <li><li>-(<span class="keyword">void) actionsheet: (uiactionsheet *) actionsheet clickedbuttonatindex: (nsinteger) buttonindex {</span></li></li> <li class="alt"><li class="alt"><span class="keyword">Switch (buttonindex) {</span></li></li> <li><li>case <span class="keyword">0:</span></li></li> <li class="alt"><li class="alt">Self.myTextFromActionSheet.text = @<span class="string">"Action destructed!"; </span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li class="alt"><li class="alt">case <span class="keyword">1:</span></li></li> <li><li>Self.myTextFromActionSheet.text = @<span class="string">"Action button 1 clicked!"; </span></li></li> <li class="alt"><li class="alt">break <span class="keyword">; </span></li></li> <li><li>case <span class="keyword">2:</span></li></li> <li class="alt"><li class="alt">Self.myTextFromActionSheet.text = @<span class="string">"Action button 2 clicked!"; </span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li class="alt"><li class="alt">case <span class="keyword">3:</span></li></li> <li><li>Self.myTextFromActionSheet.text = @<span class="string">"Cancel button clicked!"; </span></li></li> <li class="alt"><li class="alt">break <span class="keyword">; </span></li></li> <li><li><span class="keyword">default:</span></li></li> <li class="alt"><li class="alt">break <span class="keyword">; </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>}</li></li> </ol><p><p></p></p><p><p>In the above steps we have completed the use of cocoa in the uiactionsheet already have Agents. however, many times we need to write our own custom agent, how to do it?</p></p><p><p></p></p><strong><strong>Custom Proxy</strong></strong><p class="p1"><p class="p1">What we're going to do is create a view and customize a proxy implementation to update the string in this View. Above we have created a tab project to borrow the second view Inside. We drag a button to the above named changetext, the response function is-(<span class="s1">ibaction) changetext: (<span class="s1">id) sender; Click this button to enter a modal view named changetextview, We enter a string in Changetextview and update the string to second view after exiting the VIEW. How do you implement data transfer between modal view and second view? That's the agent! Who's acting? Changetextview's agent! Because we enter data directly in the changetextview, the input string needs to be fed back to the second view by the Agent. </span></span></p></p><p class="p1"><p class="p1">1. Create a new class Changetextviewcontroller and create the corresponding xib File.</p></p><p class="p1"><p class="p1">2. Declare an agreement in ChangeTextViewController.h changetextviewdelegate:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_4" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_4" width="up" height="align=" "middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "><param name=" pluginspage " value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"> <param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always">< param name= "allowfullscreen" value= "false" ><param name="flashvars" value="id=4&width=18&height=18">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">@protocol changetextviewdelegate <NSObject></li> <li></li> <li class="alt">-(<span class="keyword">void) textentered: (nsstring*) text; </span></li> <li></li> <li class="alt">@end</li> </ol> </ol><p><p><br>Like uiactionsheet, in Changetextviewcontroller we also need to add a proxy declaration:</p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_5" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_5" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=5&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">@property (assign, Nonatomic) id<changetextviewdelegate> delegate;</li> </ol> </ol><p><p></p></p><p class="p1"><p class="p1"></p></p><p><p>3, We also need to add a button in the Changetextviewcontroller.xib save, when the button is pressed back to second view, and update the String. The response function for the Save button is:</p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_6" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_6" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=6&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) savebuttonclicked: (id) Sender {</li></li> <li><li><span class="comment">//is anyone listening</span></li></li> <li class="alt"><li class="alt"><span class="keyword">if ([delegate respondstoselector: @selector (textentered:)])</span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="comment">//send The delegate function with the amount entered</span> by the user</li></li> <li><li>[delegate textEntered:textEntered.text];</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">[self dismissmodalviewcontrolleranimated:yes];</li></li> <li><li>}</li></li> </ol><p><p></p></p><p><p></p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1"><span class="s1">[delegate<span class="s1"> <span class="Apple-converted-space"> <span class="s2">textentered<span class="s1">: textentered<span class="s1">. <span class="s3"> Text<span class="s1">]; The meaning of this code is Changetextviewcontroller notification agent, textentered This event occurred, to textentered the realization of this message, That is, how to respond to this textentered event is implemented by the Proxy. In this case, Secondviewcontroller is the proxy for the Changetextviewcontroller object. so, we need to set the Secondviewcontroller to meet the Agent's Conditions. first, the declaration in SecondViewController.h follows the protocol Changetextviewdelegate. Then edit the response function of the Changetext button-(<span class="s1">ibaction) changetext: (<span class="s1">id) sender;</span> </span> </span> </span> </span> </span> </span> </span></span></span></p></p><p class="p1"><p class="p1"></p></p><strong><strong>[cpp]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_7" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_7" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=7&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) changetext: (id) Sender {</li></li> <li><li>Changetextviewcontroller *ctviewcontroller = [[changetextviewcontroller alloc] initwithnibname:@<span class="string">"  Changetextviewcontroller "bundle:nil"; </span></li></li> <li class="alt"><li class="alt"><span class="comment">//assign This class to the delegate of changetextviewcontroller,</span></li></li> <li><li><span class="comment">//remember to make Thie Viewcontroller confirm to Protocol "changetextviewdelegate"</span></li></li> <li class="alt"><li class="alt"><span class="comment">//which is delared in file ChangeTextViewController.h</span></li></li> <li><li>Ctviewcontroller.delegate = self;</li></li> <li class="alt"><li class="alt">[self Presentmodalviewcontroller:ctviewcontroller animated:yes];</li></li> <li><li>}</li></li> </ol><p><p><br>Note that ctviewcontroller.delegate = self, which implements Secondviewcontroller as a proxy for Changetextviewcontroller Objects.</p></p><p><p></p></p><p><p></p></p><p class="p1"><p class="p1">Download the source code for this article: http://download.csdn.net/detail/lovefqing/4874331</p></p><p class="p1"><p class="p1"></p></p><p class="p1"><p class="p1"><strong>If there are any errors in this article, Welcome to make bricks, Common progress, thank you!</strong></p></p><p><p>iOS design mode-delegate (delegate)</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.