Yes, the custom container view is the way to go (iOS 5 and above). You basically write your own custom container using the childViewControllers built-in property to track all child controllers. You may need your own property, say currentChildIndex , to keep track of which child controller you are currently using:
@property (nonatomic) NSInteger currentChildIndex;
Your parent controller probably has some push and pop methods for non-scroll navigation, for example:
- (void)pushChildViewController:(UIViewController *)newChildController {
Personally, I have a protocol defined for these two methods, and make sure my parent controller is configured to conform to this protocol:
@protocol ParentControllerDelegate <NSObject> - (void)pushChildViewController:(UIViewController *)newChildController; - (void)popChildViewController; @end @interface ParentViewController : UIViewController <ParentControllerDelegate> ... @end
Then, when the child wants to click on a new child, he can do it as follows:
ChildViewController *controller = ... // instantiate and configure your next controller however you want to do that id<ParentControllerDelegate> parent = (id)self.parentViewController; NSAssert([parent conformsToProtocol:@protocol(ParentControllerDelegate)], @"Parent must conform to ParentControllerDelegate"); [parent pushChildViewController:controller];
When a child wants to jump out, he can do it like this:
id<ParentControllerDelegate> parent = (id)self.parentViewController; NSAssert([parent conformsToProtocol:@protocol(ParentControllerDelegate)], @"Parent must conform to ParentControllerDelegate"); [parent popChildViewController];
And then the parent view controller is configured for pan gestures to handle panning the user from one child to another:
- (void)handlePan:(UIPanGestureRecognizer *)gesture { static UIView *currentView; static UIView *previousView; static UIView *nextView; if (gesture.state == UIGestureRecognizerStateBegan) {
It looks like you are doing up and down panning, not the left-right panning that I used above, but hopefully you get the main idea.
By the way, in iOS 6, the user interface you are asking about (sliding between views using gestures) could perhaps be done more efficiently using the built-in container controller, UIPageViewController . Just use the UIPageViewControllerTransitionStyleScroll transition UIPageViewControllerTransitionStyleScroll and the UIPageViewControllerNavigationOrientationHorizontal navigation UIPageViewControllerNavigationOrientationHorizontal . Unfortunately, iOS 5 only allows page transitions, and Apple only introduces the scroll transitions you want in iOS 6, but if that's all you need, the UIPageViewController does the job even more efficiently than what I set out above (for you no need to make any custom container calls, write gesture recognizer entries, etc.).
For example, you can drag the “page view controller” onto your storyboard, subclass UIPageViewController , and then in viewDidLoad , you need to configure the first page:
UIViewController *firstPage = [self.storyboard instantiateViewControllerWithIdentifier:@"1"]; // use whatever storyboard id your left page uses self.viewControllerStack = [NSMutableArray arrayWithObject:firstPage]; [self setViewControllers:@[firstPage] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL]; self.dataSource = self;
Then you need to define the following UIPageViewControllerDataSource methods:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { if ([viewController isKindOfClass:[LeftViewController class]]) return [self.storyboard instantiateViewControllerWithIdentifier:@"2"]; return nil; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { if ([viewController isKindOfClass:[RightViewController class]]) return [self.storyboard instantiateViewControllerWithIdentifier:@"1"]; return nil; }
Your implementation will be different (at least by different class names and different storyboard identifiers, I also allow the page view controller to instantiate the next page controller when the user requests it, and because I do not keep any strong links for them, they will be released when I go to another page ... you could just create an instance at startup, and then these before and after routines obviously did not create an instance, but rather would look for them in an array), but hopefully you get those are the idea.
But the key problem is that I have no gesture code, no custom container controller code, etc. Much easier.