傻瓜式协议和委托

发布于 2024-12-25 10:00:29 字数 1387 浏览 1 评论 0原文

我已经尝试解决这个问题了,我保证!关于这方面有大量的信息,但我仍然沉浸在抽象概念的海洋中!就像当我还是个孩子的时候,没有人能向我解释为什么一个国家不能印更多的钱并变得真正富有。我对大多数这些东西都没有那么迟钝,但出于某种原因,我无法理解这个概念,所以如果有人能尽可能慢地以居高临下的方式“与 4 岁的孩子交谈”,我真的很感激!

我认为目标行动对我来说完全有意义。这是一种允许视图与控制器对话的有用方法,而无需做很多事情。据我所知,控制器对象有效地将侦听器附加到视图对象,以便如果该视图上发生特定事件(即按下按钮),它将触发控制器方法。这在技术上可能不准确,但作为一个抽象的解释,它对我来说是有意义的。

所以顺序是:

  1. 在控制器对象中创建目标方法。
  2. 选择一个视图并以图形方式连接到该方法。
  3. 该视图上发生一个事件,该事件会触发控制器中的方法。

协议和代表让我感到困惑。我知道这与允许对象相互交谈有关,但我尝试在下面写出我(缺乏)的理解,然后将其删除,因为我认为最好不要解开我当前思维中的结,而只是擦去石板,重新开始。如果有人能花一点时间解释

  1. 使用委托/协议而不是目标/操作
  2. 的目的代码的组成部分,以及它所在的位置
  3. 使用过程时发生的事件顺序

我将永远感激不已。

从对其他解释的一些评论来看,我觉得我不是唯一一个有点迷失的人,所以希望这将具有普遍用途。非常感谢!

编辑:

好吧,我想也许如果我只是阐述我的理解,人们可以纠正我,这可能会让这变得更容易。

我的示例取自 Apple Docs,以 Window 作为视图对象,以 WindowDelegate 作为委托,其中单击关闭窗口按钮会触发“我应该关闭吗?”给代表的消息。

准则的组成部分: 窗口(视图) WindowDelegate(视图控制器?)

  1. 声明委托可以在窗口(视图)的界面部分使用的协议。
  2. 在窗口中创建委托的实例。
  3. 通过在其接口中包含 : 来声明 WindowDelegate 实现了 Window 协议。 (这里有点不稳定?)
  4. 在 WindowDelegate(视图控制器)实现部分编写必要的方法实现。
  5. 在发生特定事件时,窗口将向 WindowDelegate 发送包含特定信息的消息。
  6. WindowDelegate 将处理此问题并返回答案。

沿着正确的路线吗?

I've tried to figure this out I promise! There is a wealth of info out there on this and I'm still awash in a sea of abstract concepts! It's like when I was a kid and nobody could explain to me why a country couldn't just print more money and be really rich. I'm not this retarded with most of this stuff, but for some reason I can't wrap my head around this concept, so would really appreciate if someone could spell it out as patronizingly "talking to a 4 year old" slowly as possible!

I think target-action makes complete sense to me. It is a useful way of allowing a view to talk to a controller, without having to do a whole lot. As far as I can make out, a controller object effectively attaches a listener to the view object so that if a particular event occurs on that view (ie button pushed) it fires the controller method. This may not be not technically accurate, but as an abstract explanation it makes sense to me.

So the sequence is:

  1. A target method is created in the controller object.
  2. A view is selected and graphically connected to that method.
  3. An event happens on that view which fires the method from the controller.

Protocols and delegates have me flummoxed. I know it's something to do with allowing objects to talk to each other, but I tried writing out my (lack of) understanding so far below and just deleted it as I think it's best not to unpick the knots in my current thinking but to just wipe the slate and start afresh. If anybody could kindly spend a little time explaining the purpose of

  1. Using delegates/protocols as opposed to target/action
  2. The constituents of the code, and where it lives
  3. The sequence of events which occur when the process is being used

I would be eternally grateful.

Judging by some of the comments on other explanations, I feel I'm not the only one a little bit lost so hopefully this will be of general use. Thanks so much!

Edit:

Okay as I thought maybe if I just lay out my understanding people can correct me, and it might make this easier.

My sample is taken from the Apple Docs, with a Window as the view object and WindowDelegate as the delegate, where a click on the close window button triggers a "should I close?" message to the delegate.

Constituents of the Code:
Window (View)
WindowDelegate (View Controller?)

  1. Declare the protocol which a delegate can use in the interface section of the Window (View).
  2. Make an instance of the delegate in the Window.
  3. State that the WindowDelegate implements the Window Protocol, by including : in it's interface. (bit wobbly here?)
  4. Write the necessary implementation of the methods in the WindowDelegate (View Controller) implementation section.
  5. On a certain event the Window will send a message to the WindowDelegate with certain information.
  6. The WindowDelegate will process this and return an answer.

Anywhere along right lines?

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

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

发布评论

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

评论(4

廻憶裏菂餘溫 2025-01-01 10:00:29

这里有一大堆东西,听起来你对一些事情感到困惑。

首先,我建议您访问 Apple 开发者网站并下载《学习 Objective-C:入门》和《Objective-C 编程语言》书籍。暂时忽略目标/操作接口连接的内容,因为听起来您需要了解 Objective C 的一些基础知识。在这里您将找到有关协议等以及其他面向对象的内容的所有内容。

其次,有许多非常好的书籍将引导您逐步开发应用程序。开始 iPhone 4 开发:探索 iOS SDK 是一项备受推崇的内容。您也可以在这里了解代表的信息。

第三,花更多时间回顾文档。 SDK 文档中有很多文章从非常基础的层面概述了事物的工作原理,Apple 开发者网站也有很多文章。

There is a whole bunch of stuff here and it sounds like you have got a few things confused.

Firstly I'd suggest you go the Apple developer site and download the Learning Objective-C: A Primer and Objective-C Programming Language books. Ignore the target/action interface wiring stuff for the moment because it sounds like you need to understand some basics of Objective C. This is where you will find out all about Protocols, etc and other object orientated stuff.

Secondly there are a number of very good books out there which will take you step by step through developing an application. Beginning iPhone 4 Development: Exploring the iOS SDK is one that is well regarded. THis is also where you will find out about delegates.

Thirdly spend some more time back in the documentation. the SDK documentation has lots of articles outlining how things work from a very basic level and the Apple Developer site has lots as well.

恋竹姑娘 2025-01-01 10:00:29

委托的存在是为了帮助您避免子类化。

子类化是您应该始终尽量避免的事情,因为它是一种非常紧密的耦合形式。耦合是指两个对象相互依赖才能正常工作,耦合程度越高,更改程序就越困难(因为每当您必须更改一个对象时,您将被迫更改其他对象)依赖于它的对象)。

子类化是一种耦合形式的原因是,当您从超类创建子类时,您的子类将依赖于超类的方法(子类继承的方法)。因此,如果您必须更改超类,那么您可能必须用它更改所有子类。

假设您想要一组完全相同的对象,除了它们使用一个方法执行的操作之外。通过子类化,您将必须创建一堆不同的子类并在所有子类上覆盖同一个方法,这将是大量的子类化(以及不必要的耦合)。这就是委托的用武之地。您可以简单地创建一个类,并将其设计为拥有一个匿名类型 id 的对象,该对象将成为委托对象(我们将其称为子对象),而不是多次子类化)。该子对象将具有先前在父对象中的单个唯一方法,但现在父对象将触发子对象上的方法。

因此,我们不是多次子类化,而是创建同一类的一堆实例,并给每个实例一个不同的委托对象,我们可以这样做,因为类型 id 没有指定我们必须使用哪种类型的对象。每个不同的委托对象以不同的方式实现该方法,并且我们避免子类化。

Delegates exist to help you avoid subclassing.

Subclassing is something that you should always try to avoid, since it is a very tight form of coupling. Coupling is when two objects depend on each other to work properly, and the more coupling you have, the more difficult it is to make changes to your program (because whenever you have to change one object, you will be forced to make changes to other objects which depend on it).

The reason subclassing is a form of coupling is because when you subclass from a superclass, your subclass will depend on the superclass's methods (which the subclass inherited). So if you have to change the superclass, then you could possibly have to change all the subclasses with it.

Say you want to have a bunch of objects that are exactly the same, except for what they do with one method. By subclassing, you would have to create a bunch of different subclasses and overwrite that one method on all of them, which would be a lot of subclassing (and unwanted coupling). This is where delegates come in. Rather than subclass a bunch of times, you can simply create the one class, and design it to own an object of the anonymous type id, which will be the delegate object (we'll call it the child). This child object will have the single unique method which was previously in the parent, but now the parent will trigger the method on the child object.

So rather than subclass a bunch of times, we create a bunch of instances of the same class, and give each a different delegate object, which we can do since the type id doesn't specify which type of object we have to use. Each different delegate object implements that method differently, and we avoid subclassing.

爱冒险 2025-01-01 10:00:29

要了解委托,请查看 Cocoa 提供的委托。
例如 NSWindow 有一个名为的委托方法
-(void)windowWillClose:(NSNotification *)aNotification
如果您实现该委托方法,您将查看通知以确定哪个窗口即将关闭并对该窗口执行一些清理操作。您是 NSWindow 对象的委托,因为您正在为窗口对象做一些工作。

所有 Cocoa 委托方法的名称都包含诸如“did”、“will”、“should”等单词。它们使您的代码能够在这些对象本身执行的正常操作之外执行特殊操作-- 因此您是这些对象的委托人。

To understand delegates, take a look at the delegates provided by Cocoa.
For example NSWindow has a delegate method called
-(void)windowWillClose:(NSNotification *)aNotification
if you implement that delegate method, you look at the notification to determine which window is about to be closed and perform some cleanup for that window. You are a delegate for the NSWindow object in the sense that you are doing some work for the window object.

All of the Cocoa delegate methods have names that include words such as "did", "will", "should", etc. They give your code the ability to do special things in addition to the normal things that would done by those objects themselves -- hence you are a delegate for those objects.

窗影残 2025-01-01 10:00:29

对于代表,您可以查看此问题

对于协议,您可能需要检查此问题或看看 文档

希望这有帮助!

For delegates you might have a look at this question.

For protocols you might want to check this question or look at the documentation.

Hope this helps!

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