1

I’m working on a piece of software which takes user input, does a lot of calculations on it, and then shows numbers and graphs based on the calculation results. I consider the MVC approach as good as possible. Here are my questions:

  1. Where should I store user input (which will be shown in a table view)? Should it be stored in the needed ArrayController in the controller class or in an additional array in the model (class)? What I understand so far is that the information should be stored in the model.

  2. Where should I do all of the necessary calculations, the output of which is used for the graphs in the view? Should these be done in the model or in the view controller (class)?

  3. Does it make sense to have a separate controller class for table views in a nib? Right now I handle these data via my ArrayController in the corresponding window controller (class).

I’m asking pretty general questions because I understand that the MVC approach is independent from the code, but I have no problems with showing more specific code.

Thanks in advance.

4

3 に答える 3

2

Where do I store user input which should be shown in an table view. Should it be stored in the needed array controller in the controller class or in a additional array in the model (class)? What I understood so far is that the informations should be stored in the model.

The controller class should read it and store it in a model class. The view and model don’t speak to each other directly.

Where to do all the necessary calculations which output is taken for graphs in a view. Should that be done in the model or in the views controller (class)?

In the controller, absolutely.

Does it make sense to have an separate controller class for table views in a nib? Right now I handle these data via array controller in the corresponding window controller (class).

Yes, that makes sense. You can use a table view controller, as well — you can have as many controllers as you find useful.

于 2013-01-12T19:38:58.677 に答える
1

データ配列を計算コードと一緒にクラス(モデル)に配置できます。次に、View Controllerはユーザー入力を読み取り、それをこのモデルクラスに配置します。これは優れたオブジェクト指向設計であり、UI関連のコードがまったくない状態を保ちながら、データとその操作をうまくまとめることができます。

計算にそれほど時間がかからない場合は、メインスレッドで計算を実行して、ViewControllerがモデルメソッドを呼び出して結果を返すようにすることができます。または、データの形状によっては、モデルによって結果が他のメソッドやプロパティで利用できるようになる場合があります。

計算に時間がかかる可能性がある場合は、GCDを使用して、モデル内の優先度の低いスレッドで計算を実行することを検討する必要がありますdispatch_async()。UIを長時間ブロックしたくない場合は、1秒程度が限界になると思います。この場合、モデルの計算方法にreadyブロックを追加して、計算の準備ができたときにモデルがViewControllerに通知できるようにするのが最適です。ビューコントローラでのUIKit操作が台無しになるのを防ぐために、メインスレッドでこのブロックを実行することを忘れないでください。(私が上で述べたものの詳細やコードが必要な場合はお知らせください。ただし、そのほとんどはStack Overflowやその他の場所で簡単に見つけることができます。)

最後に、テーブルビューを含むNIB用の単一のビューコントローラが最適です。通常、UI機能(グラフなど)はUIView、余分なものではなく、サブビューに配置しUIViewControllerます。次に、サブビューをNIBに配置できます(またはプログラムでView Controllerに追加できます)。

于 2013-01-12T20:01:38.250 に答える
1

Most iOS implementations — including tons of tutorials that I have seen — do not strictly follow the MVC approach. In other words, the UIViewController subclass (or UITableViewController subclass) serves as the object that controls the view (V) and as the controller itself (C).

Technically these controllers are rather the view layer.

Your UITableView expects its controller to be the data source delegate, which means that it has to conform to the related data source protocol. Practically, the view controller often assigns self as the data source delegate (or IB assigns the File’s Owner, which usually will be the view controller).

To answer your questions:

  1. I would say that the data should be represented in the model. For supporting the table, you might use some NSArray or NSDictionary structure (with index paths) that refer to objects in the model or that contain copies of the data within the model. If you have a dedicated controller, then it should be linking the model’s data to the view’s data.

  2. All calculations that have to do with business logic should be done in the controller. If you decide to follow the established pattern of having a view controller that serves both purposes, then that would be the place for your calculations. Calculations that directly affect appearance (colors, x and y values for drawing, respectively, width and height values, etc.) should always be done in the view controller.

  3. As I said before, it is rather common to use (or abuse) the view controller (the window controller is a view controller) as the controller according to the MVC model. I have decided to follow that approach so that I can easily integrate code snippets from tutoreals or Stack Overflow answers. But in the end, it is up to you. Using the window controller, however, is only advisable when you have a single-view application. As soon as you start using navigation view controllers for pushing views, tab bars, etc., you should (ab)use the related UIViewController subclass, if you’re not using a dedicated controller object.

于 2013-01-12T19:40:51.557 に答える