1

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).

Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
  • For a general implementation, I'd go with @Mert's answer. Some examples where you'd want specific implementations : If you really have a lot of those ViewController, you may want to release them from time to time (set a limit number, or release them all on memory warning as suggested). Also if there's one main VC and the user doesn't switch very often, you can load them on demand and release then when switching to another. Maybe you can also re-use VCs of the same class and just re-configure them when switching, which would reduce the amount. – Julien Jan 08 '13 at 14:36

3 Answers3

3

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.

Mert
  • 6,025
  • 3
  • 21
  • 33
  • But you do alloc and init all the view controllers right at the start, right? Doesn't that have a big cost? And once you put a view controller in an array, how can you even release it? – Kevin Renskers Jan 08 '13 at 14:32
  • It depends what you have in init mehtod. I do have mostly nothing and do ui-tasks in viewDidLoad. It will be called before view willAppear and not while initializin the view controller unless you call view of the viewcontroller somewhere else (viewcontroller.view). You can set a break point and check your self. Like I said I have never needed to release a view controller, in case you need it just remove from that array. If you use ARC and no references to that array it will be released by iOS. When you need it then create a new one. Of course you need to bring the view to last state your self. – Mert Jan 08 '13 at 14:49
  • Maybe it is also useful. I remove the views(removeFromSuperView), which are not needed. So iOS does not try to update the UI. Then add again when I need. – Mert Jan 08 '13 at 14:52
  • Thank you for your comments. I rarely have anything in the init methods, so it should be safe. Basically this is what I've been doing until now, it's good to know I wasn't doing insane stuff :) – Kevin Renskers Jan 08 '13 at 15:25
1

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.

A-Live
  • 8,904
  • 2
  • 39
  • 74
0

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.

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45