代码在xcode ios项目中究竟做了什么? [英] What exactly does delegate do in xcode ios project?

查看:103
本文介绍了代码在xcode ios项目中究竟做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习iPhone应用程序开发,但是我很难理解什么代表实际上意味着什么?有谁能告诉我,它的作用和重要性是什么?感谢任何帮助!

解决方案

这是概念理解的一个关键概念,让您的头脑如何在前面思考技术细节很重要。简单地说,代表是一个回调。



使用代理的两个主要场景:


  1. 类或控件想要抽象出关于如何做工作的细节(如检索数据)。

  2. 允许其他人将代码挂钩到管道中。

示例:
UITableView - 表视图只是一个知道如何呈现单元格列表的控件。它处理所有重大的渲染,滚动等等...但是,它不知道如何加载您的数据。所以你实现一个数据源委托,它具有获取给定行等等的单元格数据的方法...这使你很容易。您只需使用控件并插入数据的详细信息。 UITableView会为您做所有事情...只是回答一些具体问题。一个代表回答了这些几个具体的问题。



一个文本控件 - 你添加一个文本控件到你的视图,瞧瞧!你可以输入它和所有的好。但是,如果您想在开始打字时做什么或打完字体怎么办?那么,文本控件为代理提供了一些方法,允许你钩住文本控件的执行流水线。它允许文本控制为您做所有事情,并允许您插入代码,您需要它。很多时候,有办法插入代码来决定是否允许某些东西。控制会回拨请问,是否可以做x?您可以插入代码并影响行为。



如果要创建控件或类,可以创建自己的协议,数据源代理等,以便您的控制可以专注于做什么广告。例如,假设你想创建一个任务控件。您可以:



首先,创建一个合同。嘿,如果你要为我的控制提供数据,这些是我要问你的问题。我会从那里拿走...在这种情况下,我要问你的任务数量,我会给你一个给定行号的任务。

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

在控件或类中,给消费者一种方式给我们委托数据源类,将回答控件将要求的问题。在这一点上,控制是一个纯粹的控制。它不知道如何获取您的数据。它要求一个实现合同/协议的对象(id)。 id

  @implementation XXTaskBoard 
- (void)setDelegate:(id< XXTaskBoardDelegate>)newDelegate
{
//控件存储代理,以便它可以回调并询问您的问题。
}

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

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

实现它在执行

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


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


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!

解决方案

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];
}

这篇关于代码在xcode ios项目中究竟做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆