Objective-C delegate

Source: Internet
Author: User

I have seen a very good article that allows you to instantly understand what delegation is.

When we talk about the protocol, the declarative protocol is a reserved method body. The specific implementation of the method is waiting for the person who uses the Protocol to implement it. To implement the delegate function in OC, an important member is the protocol.
There are a lot of discussions on the concept of delegation on the Internet. Some people say that delegation is a callback function in C, some say that delegation is a callback function in Java, and some say that delegation is a proxy. I personally think that it is best to define and name an object in the universe, instead of deviating from its true nature. if the definition is too far away from its nature, it is easy to mislead the audience. If we can't think of an appropriate name to define an object, we can not define it. We can simply say, "What is it, simple. Here, I don't want to discuss what delegation is, and I am afraid to mislead everyone, because it is just my personal definition. In our daily life, we often hear this. It is enough for someone, Miss Zhang, to entrust someone, Mr. Li, to do something, you only need to know that the delegate is Mr. Zhang's replacement of Miss Zhang to do something. We don't have to worry about how the two of them negotiate. In C, a method can be assigned to a pointer, So C can easily implement the delegate effect. However, we cannot regard the callback function in C as a delegate, c's callback function is just a tool for implementing the delegate effect. Java does not have a callback function similar to the callback function in C. The general method is to implement the effect similar to the callback function in C through interfaces, so do not say that there is a callback function in Java in the future. Remember, you just implemented the effect similar to the callback function in C. In oC, the delegate effect is also achieved through the Protocol. The general process is to first define a protocol and reserve null methods. First, define Mr. Li class, include the Protocol in Mr. Li class, and call the Protocol declaration method. Define Miss Zhang's type so that Miss Zhang's type complies with the previously defined protocol and implements the methods in Miss Zhang's internal protocol. Mr. Li is instantiated within Miss Zhang, and Mr. Zhang is assigned an internal agreement to Mr. Li. Then Mr. Li calls the Protocol implemented within Miss Zhang.
 
The following example is obtained from the Internet.
A definition protocol

//// Notedelegate. h // iphonedelete /// created by wangjun on 10-9-18. // copy 2010 _ mycompanyname __. all rights reserved. // define the Protocol # import <uikit/uikit. h> @ protocol notedelegate // callback function-(void) messagecallback :( nsstring *) string; @ end

B affirmed Mr. Li

<span style="font-size: 10px;">////  ManagerMessage.h//  IphoneDelete////  Created by wangjun on 10-9-18.//  Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>#import "NoteDelegate.h"@interface ManagerMessage : NSObject {    id<NoteDelegate> *noteDelegate;}@property (nonatomic,retain) id<NoteDelegate> *noteDelegate;-(void)startThread;@end

Implement Mr. Li

// Managermessage. M // iphonedelete /// created by wangjun on 10-9-18. // copy 2010 _ mycompanyname __. all rights reserved. // # import "managermessage. H "@ implementation managermessage @ synthesize notedelegate; // start a thread-(void) startthread {[nstimer scheduledtimerwithtimeinterval: 3 target: Self selector: @ selector (targetmethod :) userinfo: Nil repeats: no];}-(void) targetmethod :( nsstring *) string {If (self. Notedelegate! = Nil) {// The callback function [self. notedelegate messagecallback: @ "callback function"]; }}@ end

C affirmed Miss Zhang and brought her into compliance with the Agreement
 

////  IphoneDeleteViewController.h//  IphoneDelete////  Created by wangjun on 10-9-18.//  Copyright __MyCompanyName__ 2010. All rights reserved.//#import <UIKit/UIKit.h>#import "NoteDelegate.h"@interface IphoneDeleteViewController : UIViewController <NoteDelegate>{IBOutlet UITextField *textView;}@property (nonatomic,retain) UITextField *textView;@end

Implement Miss Zhang
 

// Iphonedeleteviewcontroller. M // iphonedelete /// created by wangjun on 10-9-18. // copyright _ mycompanyname _ 2010. all rights reserved. // # import "iphonedeleteviewcontroller. H "# import" managermessage. H "@ implementation iphonedeleteviewcontroller @ synthesize textview; // callback function-(void) messagecallback :( nsstring *) string {self. textview. TEXT = string;}-(void) viewdidload {[Super viewdidload]; self. textview. TEXT = @ "test"; managermessage * message = [[managermessage alloc] init]; // notify the call protocol message. notedelegate = self; [Message startthread]; [Message release];}-(void) didreceivemorywarning {[Super didreceivemorywarning];}-(void) viewdidunload {self. textview = nil;}-(void) dealloc {[self. textview release]; [Super dealloc];} @ end

To achieve the delegate effect, the most important thing in OC is that you need to follow a certain protocol and then assign yourself self to the delegate.

Well, after reading this article, I guess you already have some concepts and ideas. Let me extract the key part: Protocol: notedelegate as the abstract base class protagonist in C ++: iphonedeleteviewcontroller is regarded as the implementation class hosting (proxy) in C ++: managermessage is regarded as the object to be concerned in the Observer mode. Now, let's sort out the actual process semantics: Miss Zhang has something to deal with in the house, but she can't do it now. She told Mr. Li (viewdidload occurs) to ask Mr. Li to help with the schema semantics: The Observer iphonedeleteviewcontroller first implements the interface notedelegate content, and then registers the managermessage object of the interest event, notify yourself if an event of interest occurs, and the registration process occurs in the viewdidload function. After an event of interest occurs, messagecallback is called to notify the observer based on the polymorphism. I have understood it. I still hope to correct it.

Original article: http://blog.csdn.net/ma52103231/article/details/7564314

By zxzhao

Related Article

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.