Reputation: 15
I'm trying to write an app that has a menu view controller with three buttons, each corresponding to a separate view. Let's call these views v1, v2 and v3 respectively.
Normal operation of the app calls for random cycling through these three views, i.e. a user viewing v1 can transition to either v2 or v3. I am currently presenting these three view controllers modally by calling self.performSegueWithIdentifier("mySegue", sender: self)
.
I am currently navigating around these three view controllers by modally presenting them if they are not already in the stack, or else calling dismissViewControllerAnimated
on either the current view controller or it's parent depending on how far up the hierarchy the desired view is.
The problem with this is if the user presents all three views, resulting in a stack like: menu -> v1 -> v2 -> v3 and then navigates first back to v1 and then back to v3. This causes both v3 and v2 to be dismissed and then v3 to be recreated, losing any prior interaction with these view controllers.
Is there any way to create a hierarchy that would function without dismissing these view controllers yet still allow navigation between any two views?
EDIT: I'm avoiding a UITabBarController due to limits of customization, I have certain transition animations that interact with elements of my equivalent "tab bar" that I can't do with the preexisting class.
Upvotes: 0
Views: 59
Reputation: 77651
Only do this if you are certain that UITabBarController is not what you want. It sounds like it probably is though.
Yes, there are several ways of doing this. One that come immediately to mind is to have a manager class to deal with the navigation and creation of the view controllers etc...
So, where you are currently creating your first view controller and putting it on the screen instead you now create this manager class. Then you tell it top display the first view controller.
So, now you're in vc1
. (N.B. vc1
can be stored as a property of the manager class so it can keep it persistent).
In vc1 when the user taps the "go to vc2
" bit then all vc1
does is talk back to the manager (probably via a delegate method). To a method something like... [self.delegate moveToViewController2];
The manager class then checks to see if vc2
has been instantiated (and does so if it hasn't) and then displays it.
In terms of transitioning between them you can do something like this in the manager class...
- (void)moveToViewController2
{
UIViewController *viewController2 = //get or create vc2
[self transitionToViewController:viewController2 withTransition:UIViewAnimationOptionTransitionFlipFromRight]; // or whichever transition you want
}
- (void)transitionToViewController:(UIViewController *)controller withTransition:(UIViewAnimationOptions)transition
{
[UIView transitionFromView:self.window.rootViewController.view
toView:controller.view
duration:0.65
options:transition
completion:^(BOOL finished) {
self.window.rootViewController = controller;
}];
}
Upvotes: 1
Reputation: 638
I would suggest to use a UITabBarController, it does exactly what you are requesting without any extra work :)
Upvotes: 0