I have UIViewControllers A and B, they are allocated in AppDelegate. I need to apply transition to them. How to transit them without reallocating and replacing UIViews?
This code calls from my UIBarButtonItem in UINavigationController:
[UIView transitionFromView:self.view //UIViewController A
toView:appDelegate.secondViewController.view //UIViewController B
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
This method replaces UIViews in my UIViewControllers, and I can transit them back, or just don’t know how to do that. Can you tell me how to do this?
If you’re in iOS 5 world and want to jump between various view controllers, you might want to pursue View Controller Containment. Or check out WWDC 2011 session 102.
View controller containment basically assumes that you have some parent view controller which is governing the navigation between multiple child controllers. In your case, the parent view would be one with the navigation bar with the button on it.
Update:
If you pursue containment, you could create a parent view controller that has a nav bar with the button on it. When you load that view, you can add the first child view. Thus
viewDidLoadmight look like:The
configureChildjust does final configuration. As a matter of convenience, I frequently will have a UIView that I’ve set up in IB (in this case, calledchildView) which I use for setting up the frame, which gets me out of the world of manually creating frames, but you can do it any way you want:This is the action if you touch the button in the navigation bar. If you’re in the first controller, set up the second controller. If you’re in the second controller, set up the first one:
This does the basic transition (including the various containment related calls):
This method just sets up the title in the nav bar in our parent view controller based upon which child is selected. It also sets up the button to reference the other controller.
This all assumes you are going to create and destroy controllers as you jump between them. I generally do this, but use a model object to store my data so I keep whatever data I want.
You said you don’t want to do this “without reallocating and replacing UIViews”: If so, you can also change the above code to create both child view controllers up-front and change the transition to be simply jump between them:
I’ve seen it both ways, so you can do whatever works for you.