views:

32

answers:

1

I'm building my second app, and I'm trying to learn from the places I stubbed my toes on the first one.

Just as in the last app, I have sections of my app that feature a view with buttons along the bottom (basically a custom tab bar) that trigger the switching of the contents of the main, big area of the screen. One is a map view, one is a table view, one is a grid view, looking at the same objects in three different ways.

In my last app, I had each of the content options be a separate view, managed by separate ViewControllers. That worked, but there were places it was awkward. Passing data among those VCs was a little tricky (especially passing back upstream), and I was totally confused by my nested view controllers not having access to self.navigationController, for instance. It could be argued that I now know how to work with that scheme (and I do), but I'm interested in a Better Way.

I'm now thinking, maybe that whole thing should be ONE view controller, and it should have separate top-level UIView objects that it swaps in and out when the tabs at the bottom are clicked.

Problem is, two of my nested views have tables on them. So I'd need to either write REALLY complex UITableViewDelegate methods to figure out which table I'm talking about, or create separate UITableViewController subclasses to manage my table data. Either way, this just eliminated most of the simplicity I was hoping to achieve by keeping it all in one View Controller.

The other thing is, having those content views be UIViews inside the same view controller has some ramifications. There's no load time to swap views, but I'm burning memory I don't need to, if the user never visits one or more of those view alternatives.

Thoughts?

A: 

Problem is, two of my nested views have tables on them. So I'd need to either write REALLY complex UITableViewDelegate methods to figure out which table I'm talking about, or create separate UITableViewController subclasses to manage my table data.

A table view datasource/delegate does not need to be a view controller, it can be any object. So you could write two custom classes that acts as datasource/delegate for the table views.

The other thing is, having those content views be UIViews inside the same view controller has some ramifications. There's no load time to swap views, but I'm burning memory I don't need to, if the user never visits one or more of those view alternatives.

You should load the views lazily in that case, i.e. do not load anything before is is needed. And release stuff that is not needed at the moment when you receive a memory warning.

Ole Begemann
Thanks, Ole. Any advice on the main question? Should my nested views have separate controllers? How would you do it?
Dan Ray
There is no one right way, it depends entirely on your app. Apple pretty much advises us to not use UIViewControllers for views that do not take up the entire screen. But even if you want to take that advice, it often makes sense to write your own custom controllers (subclassed from NSObject) just to avoid putting too much code into the main view controller.
Ole Begemann
Huh. My interest in doing it all in one is rooted, I guess, in a desire to do it the One Right Way. Maybe I'll just do it the way I know how to do it. I DO have a deadline here... Thanks Ole!!
Dan Ray