I’ve already created a couple iPhone and iPad apps, but I’ve written them either with another supporting api or with open source libraries like Cocos2d underneath supporting the scene management and screen transitions. I don’t feel like I’ve quite figured out how screen/scene/view management is supposed to be done without these external apis. Can someone very simply, but clearly explain how an app would manage screen transitions and/or point me to a good reference that demonstrates this with as little clutter as possible?
Should I be using UIViewController-derived classes to separate the screen-by-screen functionality of my app (except for functionality that is already meant to handle multi-screen actions like navigation controllers)? And would my app essentially hand control over to one of these view controllers which would then construct my scene with its components (like labels, buttons, whatever views). How do I switch to the relevant view controller, and should I expect loadView to be called as a result? Then, when some trigger calls for moving to, say, screen B, would a call be made to ‘transition out’ my views, then call forth some other view controller to ‘set up your views’ and transition them in?
If there is a sample program that really illustrates this, preferably without interface builder, that would be ideal.
Yes.
Yes, although many VCs just get their view tree from a nib, and concern themselves with the core controller responsibilities: 1. propagating model changes into the various view fields; and 2. propagating view events back into appropriate actions on the model. I recommend getting the hang of InterfaceBuilder. It’s imperfect and annoying but it can save you hours of work and reams of code.
A number of ways:
[[self navigationController] pushViewController:theNextVC animated:YES]is a common idiom.UITabBarController.[self presentModalViewController:theNextVC animated:YES]is how. Modal stacks deeper than one or two get unwieldy though.[myMainWindow addSubview:[myInitialVC view]].Note that with the first two methods, if you want the behavior but not the UI widgets, you can hide the navigation bar or tab bar and do everything programmatically.
It will, but most of your “I’m appearing now!” logic belongs in
viewWillAppear:(called before the transition animation starts, when the view has no parent yet) andviewDidAppear:(called after the animation, when the view is fully visible.)loadViewis called once when your VC thinks the view will shortly be needed, and isn’t called again unless a low-memory condition dumps the view and it needs recreating.Override
loadViewwhen you want to completely replace nib loading with some other view tree construction method. I started out doing this, but rarely do so anymore.Override
viewDidLoadwhen you still want the normal nib loading to happen, but also want to do some view tree construction/post-processing of your own. This is what I find myself doing 99% of the time.The transitioning happens for you, if you use any of the presentation methods listed earlier. All you need to do is implement viewWill/DidAppear and friends, to be informed when it happens.
You may wish to investigate the “TheElements” sample project (search the xcode docs for “the elements”) As requested, it doesn’t use nibs; it sets up everything in code the hard way. It has good examples of using TabBarController and NavigationController, and splits responsibility among VCs in more or less the recommended way.