Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose what code to put in a View vs. a ViewController?

In Xcode, the Utility Application template makes a project with:

MainView, MainViewController

and

FlipsideView, FlipsideViewController

In my app, the two views correspond to the main UI and a preferences screen. Obviously I want the prefs to be reflected in the main UI and persisted to disk to remember settings. I know how to do that part.

The issue is, while looking at sample code of similar apps, I see that some put most of the active code in a View, leaving the ViewController little more than a stub, yet some others do it the other way around.

Is there a rule of thumb to go by when deciding where to put the bulk of my functionality?

like image 441
willc2 Avatar asked Jun 28 '09 04:06

willc2


1 Answers

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.

like image 99
David Maymudes Avatar answered Sep 29 '22 20:09

David Maymudes