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

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

问题描述

我看到这篇文章:

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

Delegate == Controller
代表和控制器可以同义使用....

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

这两个类似吗?

推荐答案

他们真的在不同的层次。

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's.delegate = controller。我们可以查看此处 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.

我不认为这意味着一个代表和一个控制器是一样的东西。

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

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

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