开发者

what is the different between class method and delegate method in iPhone

I have a questions about the iPhone application. I am the green of the iPhone application. When I read the document(PDF) download from the apple developer website (online version). I found that the document always mentions different methods of the library.

There are

1) Class method

2) Instance method

3) Delegate method

I understand the use and meaning of the instance method, which is called by a instance.

let's say the delegate methods is the connection:didReceiveAuthenticationChallenge and the class method sendSynchronousRequest:retruningResponse:error:.

However, I don't understand about the different between the class method and the delegate method. Is the class method for the whole class? or whole project? What it means of the delegate? and where should I put the code after I modify the content of the delegate? How can I call the method?

Can anyone help me. Thank you very much.

It is another question about the delegate method. And I don't how to solve the problems. Please help me. Thank you. HTTP status code = 0 (iPhone) (objecti开发者_C百科ve c)


Suppose you have a class Foo and an instance of that, Foo* foo.

Then, the class method is a method which is sent to the class:

     [Foo classMethod];

while the instance method is a method sent to the instance:

     [foo instanceMethod];

The delegate method is a method which the instance of the class calls. So, you typically implement another class Delegate with an instance Delegate* delegate, and do

    [foo setDelegate:delegate];

Then, the object foo calls the delegate method of delegate at appropriate times:

    [delegate delegateMethod];

This is a way to receive an event from the system API.


Apple provides extensive documentation on the fundamentals for Objective-C and Cocoa - if in doubt, this should be your first stop.

The Objective-C Programming Language - Class Objects:

[...] a class definition can include methods intended specifically for the class object—class methods as opposed to instance methods. A class object inherits class methods from the classes above it in the hierarchy, just as instances inherit instance methods.

Cocoa Fundamentals Guide - Delegates and Data Sources:

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
The delegating object is often a responder object—that is, an object inheriting from NSResponder in Application Kit or UIResponder in UIKit — that is responding to a user event. 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.

And some related background in The Objective-C Programming Language - Protocols:

Class and category interfaces declare methods that are associated with a particular class — mainly methods that the class implements. Informal and formal protocols, on the other hand, declare methods that are independent of any specific class, but which any class, and perhaps many classes, might implement.


A delegate method is a method that is defined in a classes delegate protocol. They are added to your class but your class must have the objects delegate protocol. They are usually used by the object but is something that you must define for the object. NSTableView and UITableView use delegate methods to populate their data. A class method is just one that you define in your interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜