【问题标题】:How to use the Model of MVC in a Q&A application?如何在问答应用中使用 MVC 模型?
【发布时间】:2014-01-03 08:05:37
【问题描述】:

我有一个应用程序可以帮助对设备进行故障排除。要进行此故障排除,我会提出建议,然后要求用户输入他们必须回答才能继续的问题。根据答案,我会加载一个包含一组新建议和问题的新视图。我不确定我是否了解 MVC 设置的模型部分。我有一个模型,其中包含建议、suggestions_images、问题、答案……然后我还将问题链接到新的模型对象。我只想知道这是否被认为是最佳实践?还是我误解了MVC的设计方案?

编辑:这是我的模型概述:

#import <Foundation/Foundation.h>

@interface troubleshootingInfo : NSObject {
    NSString *stepTitle;
    NSString *stepCount;
    NSString *description;
    NSString *imageTitle;
    NSString *descriptionImageLink;
    NSString *questionTitle;
    NSMutableArray *actionsToPerform;
    NSMutableArray *actionsStatus;
    NSMutableArray *actionsImage;
    NSMutableArray *actionImageTitle;
    NSMutableArray *logActions;
    troubleshootingInfo *nextNoObject;
    troubleshootingInfo *nextYesObject;


    // This is to be used with selections
    NSMutableArray *userInputForAction;
}

//perform function on the set values

- (int) setActionRowHieght:(int)actionID; // get row hieght for table cell
- (int) setDescriptionRowHeight; // get row hieght for table cell
- (int) setQuestionTitleHeight; // get row hieght for table header
- (int) actionCount; // the number of actions in the actionsToPerform array
- (BOOL) isAction; // is action set
- (BOOL) isDescription; // is description set
- (BOOL) isQuestion; // is question set
- (BOOL) isActionPerformed:(int)action; // check if action is performed
- (BOOL) isActionImage:(int)action; // check if action has an image
- (NSString *) updateActionPerformed:(int)action;

任何帮助都会很棒。 谢谢

编辑:根据建议修复代码(我认为)。

我现在的头文件。

@interface TroubleshootingInfo : NSObject

// values of the class
@property NSString *stepTitle;
@property NSString *stepCount;
@property NSString *description;
@property NSString *imageTitle;
@property NSString *descriptionImageLink;
@property NSString *questionTitle;
@property NSMutableArray *actionsToPerform;
@property NSMutableArray *actionsStatus;
@property NSMutableArray *actionsImage;
@property NSMutableArray *actionImageTitle;
@property NSMutableArray *logActions;
@property TroubleshootingInfo *nextNoObject;
@property TroubleshootingInfo *nextYesObject;

// This is to be used with selections
@property NSMutableArray *userInputForAction;

//perform function on the set values

- (int) actionCount; // the number of actions in the actionsToPerform array
- (BOOL) hasAction; // is action set
- (BOOL) hasDescription; // is description set
- (BOOL) hasQuestion; // is question set
- (BOOL) isActionPerformed:(int)action; // check if action is performed
- (BOOL) ihasActionImage:(int)action; // check if action has an image
- (NSString *) updateActionPerformed:(int)action;

好的,很抱歉造成混乱。我的问题与分层数据模型和表视图有关。在阅读了 Apple 的文档后,我觉得我对如何处理复杂的表有了更好的了解。我相信我在这里介绍的课程实际上需要与更复杂的数据模型分开。我会重新考虑我的设计。谢谢。

【问题讨论】:

  • 你考虑过什么?您的模型需要保存哪些类型的信息以及如何构建它?
  • @Kevin 我已经展示了我想要使用的自定义对象。我不确定这种结构是否是个好主意。
  • 多么令人兴奋,这就像工作中的同行评审!我注意到班级没有 cmets。也许添加一些 cmets 让其他编码人员确切知道它的作用可能会有所帮助。
  • 我试图缩小到我实际要求的范围。问题是基于表格的应用程序的层次结构设计。

标签: ios model-view-controller


【解决方案1】:

这看起来非常更像是Controller 样式的对象,而不是Model 对象。测验域中的Model 应该与行高之类的东西完全无关! Model:问题和答案。 也许“下一个问题”(这取决于下一个问题的选择是否由上一个答案决定,就像在自适应测试中一样)。

经典的 MVC 三元组非常简单:

  • View 反映了Model 的状态
  • Controller 构造并呈现View 和其他Controllers
  • Controller 还可以跟踪Model 的状态,并且可以修改用户体验和...
  • Controller 请求Model 修改其状态(“下一个问题”)

在 iOS 中,View 通常是 UIView 的子类,通常具有其他 UIView 元素和小部件作为成员。 Controller 将是 UIViewController 的某种形式。 Model 采用适合域的格式。

既然你在谈论行高,我建议你阅读一些关于UITableViewController 的教程。

【讨论】:

【解决方案2】:

我会做一些调整:

命名:类名应大写。

属性:大括号之间列出的 ivars 可以被属性替换。编译会自动生成getter和setter。

设计:提到 UI 布局属性的方法,如 rowHeight,不属于模型。视图控制器通常关心这类事情(例如,作为表视图的数据源)。您在这些方法中的代码很可能属于您的视图控制器。

【讨论】:

  • 感谢您的建议。我忘了把类名大写。对于 rowHeight 类型信息:我在 tableViewController 中使用它。我试图制作一个独立于它显示的数据的 tableViewController,我会将它放回控制器中(我认为这是模型的想法)。这也是一种可以接受的跟踪用户响应的方式吗?
  • 我明白你对表格视图控制器保持独立于数据的意思。更好的方法是让模型回答类似于 vc 可以放入表中的“摘要”之类的内容。 vc 可以测量它的高度(或行数等),并且仍然保持数据独立。关于用户响应,您必须指的是“userInputForAction”?似乎是合理的,如果该数组最终指向模型中的其他节点,那么作为一个由用户答案连接的问题网络是有意义的。但我需要更多地了解该应用才能提供有用的建议。
【解决方案3】:

一些观察

  • 类名使用大写字母 (TroubleshootingInfo)。
  • 不要直接使用 ivars,使用属性。应该是私有的属性(即不被其他类直接使用)可以放在() 类别中(.m 文件中的@interface TroubleshootingInfo ()
  • 模型不应该知道任何关于行高的信息。这适用于视图(有时也适用于视图控制器)。
  • 您的isAction 等布尔值听起来最好是hasActionisActionSet 等。它们询问对象是否具有 属性,而不是对象是否那个属性。

【讨论】:

  • 我明白你所说的 hasAction 是什么意思。这是可读性的好点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多