<i id='I7Jjt'><tr id='I7Jjt'><dt id='I7Jjt'><q id='I7Jjt'><span id='I7Jjt'><b id='I7Jjt'><form id='I7Jjt'><ins id='I7Jjt'></ins><ul id='I7Jjt'></ul><sub id='I7Jjt'></sub></form><legend id='I7Jjt'></legend><bdo id='I7Jjt'><pre id='I7Jjt'><center id='I7Jjt'></center></pre></bdo></b><th id='I7Jjt'></th></span></q></dt></tr></i><div id='I7Jjt'><tfoot id='I7Jjt'></tfoot><dl id='I7Jjt'><fieldset id='I7Jjt'></fieldset></dl></div>
    <bdo id='I7Jjt'></bdo><ul id='I7Jjt'></ul>
<legend id='I7Jjt'><style id='I7Jjt'><dir id='I7Jjt'><q id='I7Jjt'></q></dir></style></legend>

<small id='I7Jjt'></small><noframes id='I7Jjt'>

  • <tfoot id='I7Jjt'></tfoot>

      1. 委托在 xcode ios 项目中究竟做了什么?

        时间:2023-07-28
        <tfoot id='nAMpf'></tfoot>
            <tbody id='nAMpf'></tbody>
        • <small id='nAMpf'></small><noframes id='nAMpf'>

          <legend id='nAMpf'><style id='nAMpf'><dir id='nAMpf'><q id='nAMpf'></q></dir></style></legend>

              1. <i id='nAMpf'><tr id='nAMpf'><dt id='nAMpf'><q id='nAMpf'><span id='nAMpf'><b id='nAMpf'><form id='nAMpf'><ins id='nAMpf'></ins><ul id='nAMpf'></ul><sub id='nAMpf'></sub></form><legend id='nAMpf'></legend><bdo id='nAMpf'><pre id='nAMpf'><center id='nAMpf'></center></pre></bdo></b><th id='nAMpf'></th></span></q></dt></tr></i><div id='nAMpf'><tfoot id='nAMpf'></tfoot><dl id='nAMpf'><fieldset id='nAMpf'></fieldset></dl></div>

                  <bdo id='nAMpf'></bdo><ul id='nAMpf'></ul>
                • 本文介绍了委托在 xcode ios 项目中究竟做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我刚刚学习 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!

                  推荐答案

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

                  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. 一个类或控件想要抽象出有关如何工作的细节(例如检索数据).
                  2. 允许其他人将代码挂接到管道中.

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

                  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.

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

                  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
                  

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

                  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.
                  }
                  

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

                  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> 
                  {
                      //...
                  }
                  

                  然后,在实现中实现它

                  @implementation AppController
                  - (NSInteger*)getTaskCount
                  {
                      return [model queryTaskCount];
                  }
                  
                  - (XXTask*)getTaskForRow:(NSInteger*)rowNumber
                  {
                      return [[model tasks] getItem:(NSInteger*)rowNumber];
                  }
                  

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

                  上一篇:协议和代表之间的区别? 下一篇:NSNotificationCenter 与委派(使用协议)?

                  相关文章

                  <legend id='Nrr2P'><style id='Nrr2P'><dir id='Nrr2P'><q id='Nrr2P'></q></dir></style></legend>
                  <i id='Nrr2P'><tr id='Nrr2P'><dt id='Nrr2P'><q id='Nrr2P'><span id='Nrr2P'><b id='Nrr2P'><form id='Nrr2P'><ins id='Nrr2P'></ins><ul id='Nrr2P'></ul><sub id='Nrr2P'></sub></form><legend id='Nrr2P'></legend><bdo id='Nrr2P'><pre id='Nrr2P'><center id='Nrr2P'></center></pre></bdo></b><th id='Nrr2P'></th></span></q></dt></tr></i><div id='Nrr2P'><tfoot id='Nrr2P'></tfoot><dl id='Nrr2P'><fieldset id='Nrr2P'></fieldset></dl></div>
                • <small id='Nrr2P'></small><noframes id='Nrr2P'>

                    <tfoot id='Nrr2P'></tfoot>

                        <bdo id='Nrr2P'></bdo><ul id='Nrr2P'></ul>