委托类似于 mvc 控制器吗?

发布于 2024-12-21 04:30:32 字数 317 浏览 0 评论 0原文

我看到这篇文章:

http://www .cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-application-example/

“委托==控制器 委托和控制器这两个词可以作为同义词使用......”

我不确定他在说什么,但我对 mvc 的理解比编程中的委托更好。

这两个相似吗?

I saw this article:

http://www.cimgf.com/2008/10/01/cocoa-touch-tutorial-iphone-application-example/

"Delegate == Controller
The words delegate and controller can be used synonymously...."

I'm not sure what he is saying but I understand mvc better than I do delegates in programming.

Are the two similar?

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

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

发布评论

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

评论(2

岁吢 2024-12-28 04:30:32

他们确实处于不同的水平。

使用委托的类是一个被设计为可以由另一个对象而不是子类化来提供自定义行为的类(任何类)。

例如大多数 Cocoa 应用程序都希望在应用程序启动时执行一些自定义操作(如果不这样做,那就太无聊了)。 NSApplication 的设计不是要求每个 Cocoa 应用程序都实现 NSApplication 的自定义子类来覆盖 –applicationWillFinishLaunching:,而是设计为如果您设置它将 delegate 属性委托给有效对象,并且该对象有一个 –applicationWillFinishLaunching: 方法,它将调用该方法。

这样你就可以使用任何你喜欢的类来进行设置,而不必将其作为 NSApplication 的子类。

许多 Cocoa 类都是这样工作的,这意味着您几乎不需要对它们进行子类化来添加自定义行为。在其他一些语言和框架中,添加自定义行为的方式是通过子类化。想要用java自定义按钮吗?只需创建一个扩展 JComponent 并实现 MouseListener 的新类,然后覆盖 mouseClicked 等即可。这不是 Cocoa 的方式。

如您所知,控制器是负责协调模型和视图的对象。

碰巧,如果您需要向模型对象或视图对象的实例添加自定义行为 - 例如您有一个 NSTableView 对象,并且您希望在选择一行时添加自定义行为 - 您可能已经有一个自定义行为控制器对象,这通常是放置代码的完美位置。只需设置TableView's.delegate=controller即可。我们可以看到 这里 tableView 有一个委托方法- tableViewSelectionDidChange: 你不必创建一个NSTableView 的子类。

我不认为这意味着委托和控制器根本是同一件事。

They are really on different levels.

A Class that uses a Delegate is a Class (any Class) designed so that custom behaviour can be provided by another object instead of, say, by subclassing.

e.g. most Cocoa Apps will want to do something custom on app launch (pretty boring if they didn't). Instead of requiring every Cocoa App to implement a custom Subclass of NSApplication just to override –applicationWillFinishLaunching:, NSApplication is designed so that if you set it's delegate property to valid object, and that object has an –applicationWillFinishLaunching: method it will call that.

That way you can use any Class you like for your setup, you don't have to make it a Subclass of NSApplication.

Many Cocoa Classes work like this, meaning you hardly ever have to subclass them to add custom behaviour. In some other languages and frameworks the way you are supposed to add your custom behaviour is by subclassing. Want a custom button in java? Just create a new Class that extends JComponent and implements MouseListener then override mouseClicked, etc. This is not the Cocoa way.

A controller, as you know, is an Object responsible for coordinating the Model and the View.

As it happens, if you need to add custom behaviour to an instance of a Model Object or a View Object - say for example you have an NSTableView object and you want to add custom behaviour when a row is selected - you probably already have a custom Controller object and this is often the perfect place to put the code. Just set the TableView's.delegate = controller. We can see here that tableView has a delegate method - tableViewSelectionDidChange: You don't have to create a subclass of NSTableView.

I don't consider this to mean that a Delegate and a Controller are the same thing at all.

淡写薰衣草的香 2024-12-28 04:30:32

委托是为捕获事件而运行的后台进程。正如您从我们友好的 MVC 软件工程动态中认识到的那样,它们是控制器。事件可以是按钮、推屏、触摸、摇动等……“委托”这个词是 Coccoa 用来区分的,正如《如何指南》的作者所说,它使它们“可互换”。希望这有帮助

Delegates are the background processes running to catch an event. They are the controller as you recognize them from our friendly MVC software engineering dynamic. An event could be a button push screen touch shake etc.... Delegate is the word Coccoa uses to be distinct which as the author of your how to guide puts it makes them "interchangeable". Hope this helps

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