delegate - 概念


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 that the delegating object is about to handle or has just handled.The delegate may 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 that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

in Cocoa Core Competencies

委託有兩個角色,一個是「委託者」,另一個是「被委託者」。用中文講是這樣,用英文的話到底delegating object是委託者,還是受委託者?,應該要用英文原本的理解方式比較好。the delegate在英文裡有議員或代表的意思,在代議制度中,即被公民選票選出來的代表,負責參與政治。所以可以把它說是「the delegate」就是「被選中的代表(議員)」,那另外文中說到的「delegating object」,就把它當作「要拜託議員關說的選民」,這樣就很清楚了。我亂翻一下上面的一段:

要拜託議員關說的選民,會保留另一個人(物件)- 議員 的電話連絡資料(參考),然後在需要的時候打電話給他。這樣可以傳達訊息給議員,訊息包含選民他現在正急需處理的事情,或是剛剛處理到的事情。議員可能叫選民自己,或其他在應用程式中的人(物件),更改外觀或目狀態的方式來回應這些訊息,而且在某些情況他可以回傳會影響即將到來的事件如何被處理的數值。代議(委託)制度的最主要的價值,就是它可以讓你很容易的客製化在一個中心物件中的一些物件行為。

The delegating object is typically a framework object, and the delegate is typically a custom controller object. In a managed memory environment, the delegating object maintains a weak reference to its delegate; in a garbage-collected environment, the receiver maintains a strong reference to its delegate.

in Cocoa Core Competencies


The delegating object is often a responder object—that is, an object inheriting from NSResponder in AppKit or UIResponder in UIKit—that is responding to a user event.

in Concepts in Objective-C Programming


The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner…The delegate is almost always one of your custom objects, and by definition it incorporates application-specific logic that the generic and delegating object cannot possibly know itself.

in Concepts in Objective-C Programming


The programming mechanism of delegation gives objects a chance to coordinate their appearance and state with changes occurring elsewhere in a program, changes usually brought about by user actions. More importantly, delegation makes it possible for one object to alter the behavior of another object without the need to inherit from it.

in Concepts in Objective-C Programming





屬於動態連繫(Dymanic binding),下面有比較好的解釋,也有應用的範例情況:

The selector operator provides a way to refer to a method provided by an object, somewhat similar to a function pointer in C. It is useful because it allows you to decouple the process of calling methods on an object. For example one piece of code could provide a method, and another piece of code could apply that method to a given set of objects.

in Explanation of Cocoa @selector usage
//Test to see if an object implements a certain method:

[object respondsToSelector:@selector(methodName)]

//Store a method to later call on an object;

SEL method = @selector(methodName);
[object performSelector:method];

//Call a method on a different thread (useful for GUI work).
[object performSelectorOnMainThread:@selector(methodName)]



in Objective-C, object identifiers are of a distinct data type: id. This type is the general type for any kind of object regardless of class and can be used for instances of a class and for class objects themselves. The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of Objective-C are defined in the header file objc/objc.h.