Pregunta

If viewControllerBeforeViewController viewControllerAfterViewControllereither of the method returns nil the counter part is also called.

Is this behaviour expected behaviour ? Any way i can stop this from happening. This is what I am tried to do .

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    if ([viewController isKindOfClass:[QuestionViewController class]]){
        QuestionViewController* qvc = (QuestionViewController*)viewController;

        CVPQuestion* prevQuestion = [_surveyContext previousQuestion:qvc.question];
        return [self questionControllerWithQuestion:prevQuestion];
    }
    if ([viewController isKindOfClass:[SurveyEndedViewController class]]){
        return [self questionControllerWithQuestion:[self.questionList lastObject]];
    }
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(QuestionViewController *)viewController {

    if ([viewController respondsToSelector:@selector(question)]){
        CVPQuestion* nextQuestion = [_surveyContext previousQuestion:viewController.question];
        if (nextQuestion == nil && ![_surveyContext isAnswered:viewController.question]){
            [viewController  showToasterWithText:@"Please answer the question to continue"];
        }
        QuestionViewController* qc = [self questionControllerWithQuestion:[_surveyContext nextQuestion:viewController.question]];

        if (!qc){return [self surveyEndedController:viewController.question];}
        return qc;
    }
    return nil;
}

If viewControllerAfterViewController will get called after viewControllerBeforeViewController the notification [viewController showToasterWithText:@"Please answer the question to continue"];will be sent too.

¿Fue útil?

Solución

Looks like 'UIPageviewcontroller' is not really using the 'UIScrollViewDelegate'. So subclassing the 'UIPageViewController' and using the 'UIScrollViewDelegate' seems to do the trick to find out wether its forward swipe or backward swipe.

//Get Scroll View from PageViewController the scrollView

 - (void)viewDidLoad
     {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        for (UIView* v in [self.view subviews]){
            if ([v isKindOfClass:[UIScrollView class]]){
                _scrollView = (UIScrollView*)v;
            }
        }

        if (_scrollView){
            _scrollView.delegate = self;
        }
    }

//Record content offset when scroll view begins dragging

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _startContentOffset = _scrollView.contentOffset;
    _transitionState = SurveyPageviewCtlrTransitionInitiated;
    [_transitionStateDelegate transitionInitiated:self];
}

// Direction based on displacement

 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (_scrollView.contentOffset.x > _startContentOffset.x){
       [self.delegate forwardDrag];
     }
     else{
       [self.delegate reverseDrag];
 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top