在 Objective C 中需要帮助以更好地理解 MVC

Need help to understand MVC better in Objective C

我实际上是 Objective C 的新人。我在 Objective C 做一些项目现在只有 4 个月了。我已经尽我所能地在我的项目中实现 MVC,尽管它可能很简陋。

到目前为止我学到的基本上是(简单来说):模型:数据和逻辑。查看:图形输出。控制器:输入和更新模型/视图。 CMIIW。所以基本上控制器是中间人,它只是为模型内部的逻辑提供输入,然后接收模型的输出以将其转换为视图中的图形表示,对吗?这些信息将通过代表的使用相互传递,对吧?

所以如果有这个案例研究:

我有一个控制器,它有一个视图(将从故事板自动创建)和很多子视图。我们将这些子视图(及其模型)称为 A、B 和 C。

所以我要这些 classes:

A_ModelB_ModelA_ViewB_View 和一个 ViewController

这些代表:

A_ModelDelegates, B_ModelDelegates, A_ViewDelegates, B_ViewDelegates, 都应该放到ViewController class.

并且,假设 A_View 中有 UITextViewUIButtonB_View 中有 UITableViewUIImageView

申请流程如下: 来自 A_View 对象的 textView 将获取输入,并且其中有一个 IBAction 方法用于调用 A_ModelDelegates 中的委托之一以将输入数据发送回 ViewController 对象。这反过来会将数据发送到 A_Model 对象并对其进行处理。然后模型会将其发送回控制器以更新 A_View 对象,并将 B_Model 对象作为 tableView 和 imageView 在 [=84] 中的参数的输入=].现在 tableView 是使用 AFNetworking 库从网上序列化的。因此 B_Model 将获取数据作为 NSDictionary 对象并通过其委托将其发送回控制器。控制器将使用它来填充和更新 B_View 对象中的 tableView。

现在我的问题是:

  1. 这种MVC的思路对吗?

  2. 控制器的主要工作是否只是作为模型、视图和它们各自委托的实例(可能非常大的集合)的容器。而且它只会在它们之间传递数据?

  3. 如果我们使用 AFNetworking 从 B_Model 对象中的网络获取数据,应该有成功和失败 块,对不对?在失败块中,如果我们想提出一个警告对话框,据我所知,我们有两种不同的方法,使用 iOS 7 中的 UIAlertViewUIAlertController 中的 UIAlertController 87=] 8. 我们应该把它放在哪里?该模型?控制器?还是风景?
  4. 我的印象是它应该放在控制器中,因为 UIAlertController 有这样一行:[self presentViewController:alert animated:YES completion:nil]; 这是正确的吗?因此,我是否还必须将 UIAlertViewDelegate 放入容器中,并在需要时通过模型的委托从模型内部执行它们(例如,当模型无法从网络获取数据时,在失败框中)?如果有很多警报视图怎么办?
  5. 而对于 tableView 的序列化,我应该将数据源和委托放在哪里?控制器还是视图?模型不应该知道视图,所以数据源和委托不应该放在那里,对吗?如果我们想把它放在控制器中,如果有很多 table 视图会被放入不同的子视图中怎么办?如果有很多 table 视图,数据源和委托方法将非常大,因为每个 table 视图的所有输入/输出都必须放在那里。
  6. 还是按照上面的问题。我的序列化方式是否正确?该模型从网络中获取数据并将它们放入字典中,该字典将通过其委托传递给控制器​​。然后字典将用于填充 table 视图。

提前致谢。

我不确定我是否完全理解您的问题。也许A会在评论后改变。

Is this way of thinking for MVC is correct?

基本上,但请查看其余答案。

Is the controller's main job only as a container for the instances of models, views, and a (possibly very large collection of) their respective delegates. And it will only pass around data between them?

不,它不仅仅是数据流的粘合剂。它还是控制流的粘合剂。我觉得你的理解有误:

即使模型内部有逻辑很性感,但这是一种危险的想法。这样做可以使您的模型对应用程序更加具体。仅将模型视为数据集合,仅出于其自身的一致性而具有“代码”。即使MVC是一种常见的模式,这并不意味着你必须一直适应它。

If we are using AFNetworking to get the data form the net in the B_Model object, there should be a success and failure blocks in it, right? And in the failure block, if we want to raise an alert dialog box, AFAIK we've got 2 different ways to do it, using UIAlertView in iOS 7 and UIAlertController in iOS 8. Where should we put it? The model? The controller? Or the view?

I was under the impression that it should be put inside the controller because with the UIAlertControllerthere's this line: [self presentViewController:alert animated:YES completion:nil]; Is this correct? So must I also put the UIAlertViewDelegate in the container and execute them from inside the model when needed (eg. in the failure box when the model failed to get the data form the net) via the model's delegate? What if there are many alert views?

你应该把获取数据的代码放到controller层,而不是model层。这不仅仅是收集数据,因为可能会出现更改控制流的错误。

And for the serialization of the tableView, where should I put the data source and delegates? You do not serialize views. You serialize the model that is displayed using the table view (or another view, if you selected one item, or … You have typically one model and several views to display parts of it.)

序列化是从controller层开始的。它处理拾取数据、错误……

The controller or the view? The model shouldn't know about the view so the data source and delegate shouldn't be put there, correct? If we want to put it in the controller, what if there were lots of tableviews that would be put into a different subviews? And if there were lots of table views, the data source and delegate methods would be quite big because all of the input / output of every table views would have to be put there.

你可以有一个额外的控制器来序列化。控制器可以是委托或操作目标,但不一定是一个。控制器是一个简单的实例(可能是自定义的)class,它控制控制流。

由于模型是序列化的,不是视图,所以没有任何关联,哪个视图显示哪些信息。

Still in accordance to the question above. Is my way of serializing it correct? The model gets the data from the net and make them into a dictionary which will be passed to the controller via its delegate. And then the dictionary will be used to populate the table views.

控制器从网络获取数据,将其存储在模型中并更新视图(如果它们不能自动执行)。