<span id="Label3"></p><p><p>Original: http://blog.csdn.net/dqjyong/article/details/7685898</p></p><p><p><strong>introduction of commissioned Delegation:</strong></p></p><p><p>Delegate delegation is a simple but powerful design pattern that functions as one object in a program represents another object, or an object works with another Object. The delegate object retains a reference to another object, that is, the delegate, and the message is sent to the delegate at the appropriate Time. The delegate method for the message notification event that the delegate object is going to handle or has already processed. The delegate may respond to the message by updating the State or appearance of itself or other objects in the application, and in some cases the delegate may return a value, thereby affecting how the event will be Handled. The main value of the delegate pattern is to allow you to simply customize the behavior of certain objects through a central Object.</p></p><p><p><strong>delegate and cocoa platform</strong> :</p></p><p><p>A delegate object is a typical platform object, and a delegate is a typical custom controller Object. In a managed memory environment, a delegate object retains a weak reference to its delegate, whereas in a garbage-collected environment, the recipient is the delegate object that retains a strong reference to its Delegate. Examples of <strong>Delegated delegation</strong> are found in foundation, UIKit, appkit, and other R Cocoa and Cocoa touch platforms.</p></p><p><p>Take an instance of the Nswindow class on the AppKit platform as an example of a delegate object. The Nswindow class declares the protocol, and the protocol contains the <code>windowShouldClose:</code> Method. When the user clicks the Close button on the form, the Form object sends <code>windowShouldClose:</code> the delegate to it and requests the delegate to close the Form. The delegate returns a Boolean value that controls the behavior of the form Object.</p></p><p><p></p></p><p><p><strong>Let's talk about how to use delegate in iOS</strong>:</p></p><p><p><strong>first, declare delegate</strong>. For example:</p></p><p><p>Returns the delegate entered by the user</p></p><p><p>@protocol enterdelegate <NSObject></p></p><p><p>@required//this can be required, or it can be optional</p></p><p><p>-(void) entered: (nsinteger) amount;</p></p><p><p>@end</p></p><p><p>In the declaration of the delegate, we only defined a method that would give Rootviewcontroller a Nsinteger object obtained from the user Input.</p></p><p><p><strong>second, Define a delegate object in Rootviewcontroller</strong> . The following code:</p></p><p><p>@interface Enterviewcontroller:uiviewcontroller {<br><br>Iboutlet Uitextfield *inputtextfield;<br><br>ID < enterdelegate > delegate;<br><br>}<br>-(ibaction) Cancel<br>-(ibaction) save;<br>@property (nonatomic,assign) ID delegate;<br>@end</p></p><p><p>In the code above, "id < enterdelegate > delegate" declares the ID object and points to a reference to the delegate already Defined. Then we need to add some attributes to the ID object so that Enterviewcontroller can access it.</p></p><p><p><strong>next</strong> , <strong>implement the Delegate method</strong> . Because the delegate entered: method is required, the method needs to be Implemented.</p></p><p><p>-(void) entered: (nsinteger) Amount<br>{<br>Inputlabel.text = [nsstring stringwithformat:@ "%i", amount];<br>}</p></p><p><p><strong></strong></p></p><p><p><strong>finally, The delegate object is completed to send a message to the Delegate. That is, in the Enterviewcontroller implementation File. M completes some of the methods defined above</strong> .</p></p><p><p>-(ibaction) savepressed<br>{<br>Determine if a delegate exists entered method<br>If ([delegate respondstoselector: @selector (entered:)])<br>{<br>Send the delegate method, the parameter of the method is the input of the user<br>[delegate Entered:[inputtextfield.text intvalue];<br>}<br><br>[self dismissmodalviewcontrolleranimated:yes];<br>}</p></p><p><p>The Savepressed method is associated with the Save button in the Toolbar. The method is triggered when the user completes the digital input and clicks Save.</p></p><p><p>====================================================</p></p><p><p>The process above is how to use the delegate process, although the function is very simple.</p></p><p><p>From the implementation of the delegate method, the implementation of the delegate method is done in another class, that is, the internal exposure of the delegate class, contrary to the hidden implementation principle we generally adhere to.</p></p><p><p>It can be seen from the definition of the delegate class that there is a certain similarity between the delegation and the Agreement. are declared using the protocol Keyword.</p></p><p><p>, and the methods are optional and required, all of them need to implement required method and optional method of Invocation. The difference is that you need to define a delegate object in the class where the delegate object resides, and that is the ID type.</p></p><p><p><strong>But delegate is not related to Protocol. Delegate itself should be called a design pattern, is to put a class itself to do a part of the thing, let another class (also can be itself) to complete, and the actual work of the class is Delegate. While <strong>protocol is a syntax, its main goal is to provide interfaces to the classes that comply with the protocol, which provides a convenient way to implement the delegate pattern</strong> .</strong></p></p><p><p>IOS thoroughly learns to use delegate</p></p></span>
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