delegate 在 xcode ios 项目中到底做了什么?

发布于 2024-12-01 14:43:25 字数 68 浏览 1 评论 0原文

我刚刚学习iPhone应用程序开发,但我很难理解委托的真正含义是什么?谁能举例说明它的作用以及它的重要性吗?感谢您的帮助!

I have just been learning iPhone apps development but I have a hard time in understanding what delegate actually means? Can anyone tell me with example what it does and how important it is? Thanks for any helps!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-12-08 14:43:25

这是一个需要从概念上理解的关键概念,因此在技术细节之前了解如何思考它很重要。简单来说,委托就是一个回调。

使用委托的两个主要场景:

  1. 类或控件想要抽象出如何工作的细节(例如检索数据)。
  2. 允许其他人将代码挂钩到管道中。

示例:
UITableView - 表格视图只是一个知道如何呈现单元格列表的控件。它处理渲染、滚动等所有繁重的工作……但是,它不知道如何加载数据。因此,您实现了一个数据源委托,它具有获取给定行的单元格数据等的方法......这使您变得轻松。您只需使用该控件并插入数据的具体信息即可。 UITableView 将为您做一切...只需回答一些具体问题。一位代表回答了这几个具体问题。

文本控件 - 您将文本控件添加到视图中,瞧!你可以输入它,一切都很好。但是,如果您想在他们开始打字或完成打字时执行某些操作,该怎么办?那么,文本控件提供了一个委托,其中的方法允许您挂接到文本控件的执行管道中。它允许文本控件为您做所有事情,并允许您在需要的地方插入代码。很多时候,可以通过插入代码来决定是否允许某些操作。控制会回拨并询问,我可以做 x 吗?您可以插入代码并影响行为。

如果您正在创建控件或类,则可以创建自己的协议、数据源委托等...这样您的控件就可以专注于执行广告中的操作。例如,假设您想要创建一个任务控件。您可以:

首先,签订合同。嘿,如果你要提供数据供我控制,这些就是我要问你的问题。我将从那里开始...在这种情况下,我将询问您任务的数量,并且我将让您给我一个给定行号的任务。

@protocol XXTaskBoardDelegate <NSObject>
-(NSInteger*)getTaskCount;
-(XXTask*)getTaskForRow:(NSInteger*)rowNumber;
@end

在控件或类中,为使用者提供一种方法来向我们提供委托数据源类,该类将回答控件将提出的问题。此时,该控件是一个纯控件。它对您如何获取数据一无所知。它要求一个实现合同/协议的对象(id)。 id

@implementation XXTaskBoard
- (void)setDelegate:(id<XXTaskBoardDelegate>)newDelegate
{
    // the control stores the delegate so it can callback and ask you questions.
}

然后,对于委托类,在标头中声明您实现该正式协议
并在实现 m 文件中提供代码。

@interface AppController : NSObject<XXTaskBoardDelegate> 
{
    //...
}

然后在实现中实现

@implementation AppController
- (NSInteger*)getTaskCount
{
    return [model queryTaskCount];
}

- (XXTask*)getTaskForRow:(NSInteger*)rowNumber
{
    return [[model tasks] getItem:(NSInteger*)rowNumber];
}

It's a key concept to understand conceptually so getting your head around how to think about it ahead of the technical details is important. Simply put, a delegate is a callback.

Two main scenarios to use delegates:

  1. A class or control wants to abstract out the details on how to do work (like retrieve data).
  2. Allow others to hook code into a pipeline.

Examples:
UITableView - a table view is just a control that knows how to render a list of cells. It handles all the heavy lifting of rendering, scrolling, etc... But, it has no idea how to load your data. So you implement a datasource delegate which has methods to get the cell data for a given row etc... That makes it easy on you. You just use the control and plug in the specifics for your data. The UITableView will do everything for you ... just answer a few specific questions for. A delegate answers those few specific questions.

A text control - you add a text control to your view and voila! you can type in it and alls good. But what if you want to do something when they start typing or when they're done typing? Well, the text control offers a delegate with methods that allow you to hook into the execution pipeline of the text control. It allows the text control to do everything for you and allows you to interject code where you need it. Many times, there's way to interject code to make a decision on whether something is allowed. The control will call back and ask, should I be able to do x? You can interject code and influence the behavior.

If you're creating a control or class, you can create your own protocol, datasource delegates etc... so your control can focus on doing what's advertised. For example, let's say you wanted to create a task control. You could:

First, create a contract. Hey, if you're going to provide data for my control, these are the questions I'm going to ask you. I'll take it from there... In this case, I'm going to ask you the number of tasks and I'm going to have you give me a task given the row number.

@protocol XXTaskBoardDelegate <NSObject>
-(NSInteger*)getTaskCount;
-(XXTask*)getTaskForRow:(NSInteger*)rowNumber;
@end

In the control or class, give the consumer a way to give us the delegate datasource class that will answer the questions the control will ask. At this point, the control is a pure control. It knows nothing about how you get your data. It's asking for an object (id) that implements a contract/protocol. id

@implementation XXTaskBoard
- (void)setDelegate:(id<XXTaskBoardDelegate>)newDelegate
{
    // the control stores the delegate so it can callback and ask you questions.
}

Then, for the delegate class, in the header declare you implement that formal protocol
and in the implementation m file you provide the code.

@interface AppController : NSObject<XXTaskBoardDelegate> 
{
    //...
}

then, implement it in the implementation

@implementation AppController
- (NSInteger*)getTaskCount
{
    return [model queryTaskCount];
}

- (XXTask*)getTaskForRow:(NSInteger*)rowNumber
{
    return [[model tasks] getItem:(NSInteger*)rowNumber];
}
葵雨 2024-12-08 14:43:25

委托是另一个类可以向其传递消息的对象。实际上,委托类必须遵守委托协议。

例如,我们将采用表视图控制器的子类。这是您的表视图的委托。首先,您通过执行以下操作来定义它是一个表视图委托:

MyTableViewController : UITableViewController <UITableViewDelegate>

这表示类 MyTableViewControllerUITableViewController 的子类,并且符合 UITableViewDelegate协议。

设置[tableView setDelegate:self](或在IB中这样定义)然后将self对象传递给tableview,以便tableview向其发送消息。

它发送的主要消息是 didSelectRowAtIndexPath 消息,它告诉您的类用户已按下表视图单元格。

因此,接受单击事件的对象(表视图)会将单元格已被单击的消息传递给委托对象(在本例中是您的 MyTableViewController 对象)。

委托协议的存在是为了确保委托对象具有处理消息所需的方法。委托协议中的方法可以是@可选的,也可以是强制的。任何可选方法都不必定义。在您的 MyTableViewController 类中,方法 didSelectRowAtIndexPath 是可选的 - 您不必拥有它。如果表视图找不到该方法,它就不会调用它。

但是,cellForRowAtIndexPath 是必需的,没有它,您的应用程序将无法编译。

我希望这对您有所帮助并且简单明了。如果您需要更多信息,请告诉我。

A delegate is an object that another class can pass messages to. In practice delegate classes have to conform to a delegate protocol.

For instance we will take a subclass of a table view controller. This is a delegate for your table view. First you define that it is a table view delegate by doing this:

MyTableViewController : UITableViewController <UITableViewDelegate>

This says that class MyTableViewController is a subclass of UITableViewController and CONFORMS to the UITableViewDelegate protocol.

Setting [tableView setDelegate:self] (or defining it as such in IB) then passes the self object to the tableview in order for the tableview to send messages to it.

The main message it sends is the didSelectRowAtIndexPath message which tells your class that the user has pressed a table view cell.

So the object that takes the click event (the table view) passes on the message that the cell has been clicked to the delegate object (which in this case is your MyTableViewController object).

Delegate protocols exist so that you can make sure that the delegate object has the necessary methods to deal with your messages. Methods in a delegate protocol can be @optional or enforced. Any methods that are optional don't have to be defined. In your MyTableViewController class the method didSelectRowAtIndexPath is optional - you don't have to have it. If the table view doesn't find the method it just won't call it.

However the cellForRowAtIndexPath is necessary and without it your app won't compile.

I hope this helps and is straightforwards for you. If you need any more info let me know.

陪我终i 2024-12-08 14:43:25

委托只是从某些事物中获取回调的方式。您将委托(指向符合协议的对象的指针)传递给某个对象,当它为您提供新数据时,或者当某个事件发生时,某个对象会对委托进行方法调用。

例如,当事件发生时,例如您的应用程序被置于后台或应用程序即将终止,UIApplication 对象将调用您的应用程序委托以使其知道。当 CLLocationManager 有新的 GPS 位置时,将调用您的委托将新位置传递给它。 UITableViews 调用它们的委托来获取 UITableViewCells 以在表中显示。 iOS 中委托有很多用途。

Delegates are just way of getting callbacks from something. You pass a delegate (a pointer to an object that conforms to a protocol) to something and when it has new data for you or when an event occurs that something make a method call on the delegate.

For example, when events occur, like your app is put into the background or the app is about to terminate the UIApplication object will call your application delegate to let it know. When a CLLocationManager has a new GPS position is will call your delegate to pass it the new position. UITableViews call their delegates to get UITableViewCells to display in the table. There are many uses of delegates in iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文