委托类似于 mvc 控制器吗? [英] Is a delegate analogous to a mvc controller?

查看:16
本文介绍了委托类似于 mvc 控制器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章:

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

"委托==控制器委托和控制器这两个词可以同义使用......"

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

我不确定他在说什么,但我比编程中的代表更了解 mvc.

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

两者相似吗?

推荐答案

它们确实处于不同的级别.

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.

例如大多数 Cocoa 应用程序都希望在应用程序启动时做一些自定义的事情(如果他们没有,那就很无聊了).不是要求每个 Cocoa 应用程序实现 NSApplication 的自定义子类只是为了覆盖 –applicationWillFinishLaunching:NSApplication 被设计成这样,如果你设置它delegate 属性给有效对象,该对象有一个 –applicationWillFinishLaunching: 方法,它将调用它.

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.

这样你就可以在你的设置中使用任何你喜欢的类,你不必让它成为 NSApplication 的子类.

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

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

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.

碰巧的是,如果您需要向模型对象或视图对象的实例添加自定义行为 - 例如,您有一个 NSTableView 对象,并且您想在选择一行时添加自定义行为 - 您可能已经有一个自定义的 Controller 对象,这通常是放置代码的最佳位置.只需设置 TableView 的.delegate = 控制器.我们可以看到 此处 表明 tableView 具有委托方法 - tableViewSelectionDidChange: 您不必创建 NSTableView 的子类.

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.

我不认为这意味着 Delegate 和 Controller 是一回事.

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

这篇关于委托类似于 mvc 控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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