MVC - Cocoa 界面 - Cocoa 设计模式书
于是我开始读这本书: http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/ dp/0321535022
在第 2 章中,它解释了 MVC 设计模式,并给出了我需要一些说明的示例。
这个简单的示例显示了包含以下字段的视图: 小时费率、工作时间、标准时间、工资。
该示例分为 3 部分: 视图 - 包含一些文本字段和一个表(该表包含员工数据列表)。
控制器 - 由 NSArrayController 类组成(包含 MyEmployee 数组)
模型 - 描述员工的 MyEmployee 类。 MyEmployee类有一个方法,根据计算逻辑返回工资, 和属性符合视图 UI 控件。 MyEmployee 继承自 NSManagedObject。
有几件事我不确定: 1.在MyEmplpyee类实现文件中,计算方法使用如下语句获取类属性 “ [[self valueForKey:@"hourlyRate"] floatValue];"但是,标头内没有名为 hourlyRate 的数据成员或任何视图字段。
我不太确定它是如何工作的,以及它如何从正确的视野中获取值。 (是否必须与视图中的字段名称相同)。 也许连接是使用界面生成器以某种方式进行的,并且没有在书中显示?
更重要的是: 2.它如何将视图与模型分开?假设,正如书中暗示的可能发生的情况,我决定有一天删除视图中的一个字段。 据我了解,这意味着更改 MyEmplpyee 中工资方法的工作方式(因为我们少了一个字段),并从同一类中删除一个属性。 那么,如果更改一个视图会影响另一个视图,那么如何将视图与模型分开呢?
我想我弄错了...有什么意见吗? 谢谢
So I started reading this book:
http://www.amazon.com/Cocoa-Design-Patterns-Erik-Buck/dp/0321535022
On chapter 2 it explains about the MVC design pattern and gives and example which I need some clarification to.
The simple example shows a view with the following fields:
hourlyRate, WorkHours, Standarthours , salary.
The example is devided into 3 parts :
View - contains some text fiels and a table (the table contains a list of employees' data).
Controller - comprised of NSArrayController class (contains an array of MyEmployee)
Model - MyEmployee class which describes an employee.
MyEmployee class has one method which return the salary according to the calculation logic,
and attributes in accordance with the view UI controls.
MyEmployee inherits from NSManagedObject.
Few things i'm not sure of :
1. Inside the MyEmplpyee class implemenation file, the calculation method gets the class attributes using sentence like
" [[self valueForKey:@"hourlyRate"] floatValue];" Howevern, inside the header there is no data member named hourlyRate or any of the view fields.
I'm not quite sure how does it work, and how it gets the value from the right view field. (does it have to be the same name as the field name in the view).
maybe the conncetion is made somehow using the Interface builder and was not shown in the book ?
and more important:
2. how does it seperate the view from the model ? let's say ,as the book implies might happen, I decide one day to remove one of the fields in the view.
as far as I understand, that means changing the way the salary method works in MyEmplpyee (cause we have one field less) , and removing one attribute from the same calss.
So how is that separate the View from the Model if changing one reflect on the other ?
I guess I get something wrong... Any comments ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
讨论了
valueForKey:
方法实现此处。请注意,valueForKey:
实际上可以直接访问ivars,而无需调用任何方法。如果您从 NSTableView 中删除一列,则不必将其从模型对象类中删除。它仍然在那里,只是不显示。
The
valueForKey:
method implementation is discussed here. Note thatvalueForKey:
can actually access the ivars directly without calling any methods.If you remove a column from your NSTableView you don't have to remove it from your model object class. It's still there, it just doesn't get displayed.