This article covers how to give View Controllers managed by a Page View Controller the ability to know when they are being ‘left’ or ‘entered’ by the page controller.

Protocol

To start off I made PageVieWTransitionCallback.h to create a protocol:

#import <Foundation/Foundation.h>

//DELEGATE DEFINITION
@protocol PageViewControllerResponseDelegate
-(void)transitionResponseLeave;
-(void)transitionResponseEnter;
@end

Implement protocol as above in your view controllers

With that done implement this within any view controllers that you will be using in the page view controller that require notifications. Quick checklist:

1. Add header declaration
2. Add protocol to class declaration
3. Add transitionResponseLeave and transitionResponseEnter methods in the implementation file (or make these optional)

Page View Controller

Then you will need to make your Page View Controller notify any view controllers with this delegate:

    -(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
        //Notifies child view controllers if they have left or not
        UIViewController* vc = (UIViewController*)previousViewControllers[0];
        
        if(completed){
            //Notify VC
            if([vc conformsToProtocol:@protocol(PageViewControllerResponseDelegate)]){
                id<PageViewControllerResponseDelegate> pageViewDelegate = (id<PageViewControllerResponseDelegate>)vc;
                [pageViewDelegate transitionResponseLeave];
            }
            
            if([[[pageViewController viewControllers] lastObject] conformsToProtocol:@protocol(PageViewControllerResponseDelegate)]){
                id<PageViewControllerResponseDelegate> pageViewDelegate = (id<PageViewControllerResponseDelegate>)[[pageViewController viewControllers] lastObject];
                [pageViewDelegate transitionResponseEnter];
            }
        }
    }

I’ve only lightly tested this code but it seems to be working well.

No Comments

There are no comments related to this article.

Leave a Reply

Your email address will not be published. Required fields are marked *