Question

I am building an iPhone app that has a complex view at it's core. This view is expensive to render and would ideally stay on the screen without 'interruption' while user is interacting with the app. Around and on top of this view I need to show several "sets" of controls: buttons, labels, text fields, etc. Depending on what the user does, one set of controls should disappear and another set come into view. What is the best way to manage these transitions?

I could have a different UIViewController for each state, but then who owns the central complex view? Can multiple controllers share the same UIView in such a way that I can push/present a new controller without the shared view re-initializing/flashing?

Currently, what I have is a spaghetti mess of:

if (state == x) 
    setHidden:YES
    setHidden:YES
    ... etc ...
    setHidden:NO
    setHidden:NO
    ... etc ...
} else if (state = y) {
    setHidden:NO
    setHidden:NO
    ... etc ...
    setHidden:YES
    setHidden:YES
    ... etc ...
}

It's already barely manageable and the whole app will eventually end up inside one view controller if someone doesn't suggest something better.

I think: Managing multiple UIViews from one UIViewController and Sharing a UIView across multiple UIViewControllers without background flashing were asking very similar questions, but I didn't understand how to apply the answers to my problem.

Was it helpful?

Solution

After some more searching I found the answer to my question here: Storyboards With Custom Container View Controllers

The solution involves embedding several container views 'around' the central UIView. Each of those containers changes the sets of controls loaded by swapping embedded UIViewControllers via custom segues. The logic for individual subview behavior is encapsulated inside embedded view controllers and the logic for swapping them is all inside the custom view container class. Very clean and extensible. The site above does a much better job of explaining it. Here's a link straight to the example on GitHub: https://github.com/mluton/EmbeddedSwapping

Some Apple docs that I also found to be helpful:

Creating Custom Container View Controllers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top