Как выбрать что код вставить Представление по сравнению с ViewController?

Инженеры-программисты не должны работать с ребятами из информатики

Их различия: SE заботятся о возможности повторного использования кода, в то время как CS просто разбираются с кодом SE, заботятся о производительности, в то время как CS просто хотят, чтобы дела делались сейчас, а SE заботятся о Вся структура, в то время как CS не дают жеребьевки ...

7
задан Mick MacCallum 20 July 2014 в 16:52
поделиться

3 ответа

One way to decide: if your app gets a low-memory warning, the default behavior is that any view that isn't currently visible may be destroyed. This means that if you have any state information that you can't easily re-create, you'd better not keep it in your view.

So it depends what the bulk of your functionality is doing: if it's maintaining information that the user created, it needs to be in the view controller.

2
ответ дан 7 December 2019 в 12:24
поделиться

If you want to be a MVC purist, things like view-switching and event handling should go in the controller, and the view-building code in the view.

But it's ok to put some app logic in the view, if you are consistent across the whole app.

2
ответ дан 7 December 2019 в 12:24
поделиться

There's already several questions at StackOverflow covering model-view-controller. For instance, see What goes into the "Controller" in "MVC"?.

In your specific scenario, the preferences themselves are a kind of model that keeps track of user's preferences. Saving and loading those preferences is the duty of the controller. Displaying those preferences and giving the user a way to change them is the duty of the view. And finally, when a user changes those preferences the view reports this to the controller, which makes the appropriate changes to the model.

1
ответ дан 7 December 2019 в 12:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: