Sliding menus: how to deal with actually changing view controllers

1k Views Asked by At

It seems 50% of all iPhone apps are using Facebook-like sliding menus these days. I've also created a few apps with this UI, using the ViewDeck library (https://github.com/Inferis/ViewDeck). The left view is a UITableView, clicking on an item changes the center view.

I've been struggling with good "menu management" though. Do you create an NSArray with all the view controllers? Is it better to lazy load one at a time? How do you deal with memory? Not really sure what the best way is while keeping memory usage as low as possible.

When I look at these sliding menu libraries, there's never a full fledged example app with a working menu and multiple controllers. Like I said, I've created a couple apps using ViewDeck, but the actual changing of the view controllers always feels clunky and not optimal at all (array with all instantiated view controllers).

3

There are 3 best solutions below

4
On BEST ANSWER

I use an Array for view controllers not for views. The views are loaded, when user selects the cell which points that view controller. So it is lazy loading. If you think you need to be careful about memory, then on memory warning you can release the view controllers which you do not need for now.

Of course it depends what do you have in that controllers but generally (standard UI) you don't need to release them. I have never needed.

0
On

What I have done is created a container view controller, with the sidebar menu at the bottom of the view, meaning it is not visible. On top of that, I have a contentView (which is just a UIView), I have a UINavigationController, which manages the UIViewControllers, with the navigation bar set to hidden. I then pass a reference to this UINavigationController to the sidebar menu, which is just a subclass of UIView with a UITableView, and this is where the UIViewControllers are loaded. When the user selects a row, I alloc/init the view controller. So when the new UIViewController is pushed onto the stack, it is pushed onto the UINavigationController in the content view.

0
On

First of all, note I've never used Facebook app or ViewDeck, based on the demo video it's clear what the library is used for.

I can suggest you to look for different patterns, e.g. there's a book Pro Objective-C Design Patterns for iOS, it describes a pretty simple mediator pattern which is basically the controller of controllers.

The memory management completely depends on the task, I don't see any reason to initialize all the view controllers at once as their views might be loaded while not displayed. Also views are not always the most resource-hungry part of the controller. Such an approach would require a very strict and careful controllers development, you'd also have to make them reusable which is not so easy.

Instead I would try to make it as as flexible and simple as possible: an array of used view controllers, the next is loaded and added there on demand. It should be something very similar to UINavigationController controllers stack. UINavigationController itself is a good sample to research, you'll not have the source code but you'll have all the headers and the internal logic is not as difficult.