简体   繁体   English

iPhone中的类方法和委托方法有什么区别

[英]what is the different between class method and delegate method in iPhone

I have a questions about the iPhone application. 我对iPhone应用程序有疑问。 I am the green of the iPhone application. 我是iPhone应用程序的绿色代表。 When I read the document(PDF) download from the apple developer website ( online version ). 当我阅读文档(PDF)时,可以从Apple开发者网站下载( 在线版本 )。 I found that the document always mentions different methods of the library. 我发现该文档总是提到该库的不同方法。

There are

1) Class method 1)课堂方法

2) Instance method 2)实例方法

3) Delegate method 3)委托方式

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: . 假设委托方法是connection:didReceiveAuthenticationChallenge sendSynchronousRequest:retruningResponse:error:和类方法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) (objective c) HTTP状态码= 0(iPhone)(目标c)

Suppose you have a class Foo and an instance of that, Foo* foo . 假设您有一个Foo类和一个实例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: 而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 因此,通常使用实例Delegate* delegate来实现另一个类Delegate ,然后执行

    [foo setDelegate:delegate];

Then, the object foo calls the delegate method of delegate at appropriate times: 然后,对象foo调用的委托方法delegate在适当的时间:

    [delegate delegateMethod];

This is a way to receive an event from the system API. 这是一种从系统API接收事件的方法。

Apple provides extensive documentation on the fundamentals for Objective-C and Cocoa - if in doubt, this should be your first stop. Apple提供了有关Objective-C和Cocoa基础知识的大量文档-如有疑问,这应该是您的第一站。

The Objective-C Programming Language - Class Objects : Objective-C编程语言-类对象

[...] 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. 委托对象通常是响应用户事件的响应者对象,即从Application Kit中的NSResponder或UIKit中的UIResponder继承的对象。 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 : 以及《 Objective-C编程语言-协议》一些相关背景知识:

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. NSTableView和UITableView使用委托方法填充其数据。 A class method is just one that you define in your interface. 类方法只是您在界面中定义的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM