UIPageviewController以编程方式更改页面

时间:2014-12-17 09:32:26

标签: ios objective-c uipageviewcontroller

我尝试使用不同的页面实现UIPageViewController(每个页面都是一个带有4个按钮的问题)。它可以与滑动/滚动手势一起使用。

因此,我试图禁用滑动/滚动手势并用void方法(programmaticaly)替换它。当我点击一个按钮时,问题就会改变。

我不知道为什么,但它不起作用。

我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Create the data model
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
    _questionsDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    nbQuestions = [[self.questionsDictionary objectForKey:@"QUESTIONS"] count];

    // Create page view controller
   self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
   self.pageViewController.dataSource = nil;
   [self.pageViewController setDataSource:self];
   [self.pageViewController setDelegate:self];

   QuestionsContentViewController *startingViewController = [self viewControllerAtIndex:0];
   viewControllers = @[startingViewController];
   [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

   [self addChildViewController:_pageViewController];
   [self.view addSubview:_pageViewController.view];
   [self.pageViewController didMoveToParentViewController:self];

   //disable scroll
   for (UIScrollView *view in self.pageViewController.view.subviews) {
      if ([view isKindOfClass:[UIScrollView class]]) {
        [view setScrollEnabled:NO];
      }
   }
}






#pragma mark -
- (QuestionsContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    NSLog(@"viewControllerAtIndex INDEX : %lu ---> %ld", (unsigned long)index, (long)[[self.questionsDictionary objectForKey:@"QUESTIONS"] count]);
    if (index < [[self.questionsDictionary objectForKey:@"QUESTIONS"] count])
    {
        // Create a new view controller and pass suitable data.
        QuestionsContentViewController *questionsContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"QuestionsContentViewController"];
        [questionsContentViewController setTitleText:[NSString stringWithFormat:@"Question n°%lu", (unsigned long)index + 1]];
        [questionsContentViewController setThemeText:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"theme"]];
        [questionsContentViewController setQuestionText:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"question"]];
        [questionsContentViewController setPurposal1Text:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"purposal1"]];
        [questionsContentViewController setPurposal2Text:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"purposal2"]];
        [questionsContentViewController setPurposal3Text:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"purposal3"]];
        [questionsContentViewController setPurposal4Text:[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"purposal4"]];
        [questionsContentViewController setResponseValue:[[[[self.questionsDictionary objectForKey:@"QUESTIONS"] objectAtIndex:index] objectForKey:@"response"] integerValue]];

        [questionsContentViewController setPageIndex:index];

        return questionsContentViewController;
    }

    //NSLog(@"NIL *********************************");
    return nil;
}


- (ResultViewController *)displayResultViewController
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSInteger theScore = [defaults integerForKey:@"score"];


    ResultViewController *resultViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ResultViewController"];
    [resultViewController setScoreText:[NSString stringWithFormat:@"%ld", (long)theScore]];

    return resultViewController;

}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((QuestionsContentViewController *) viewController).pageIndex;
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index --;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((QuestionsContentViewController *) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

    index ++;
    if (index == ([[self.questionsDictionary objectForKey:@"QUESTIONS"] count] - 1)) {
        return [self displayResultViewController];
    }

    return [self viewControllerAtIndex:index];
}


- (void)passToTheNextQuestion:(NSUInteger)pageIndex {

    //HERE I'M TRYING TO PASS TO THE NEXT VIEW
    QuestionsContentViewController *nextViewController = [self viewControllerAtIndex:3];
    if (nextViewController == nil) {
        NSLog(@"viewController -- nil ****************************");
        return;
    }

    [self.pageViewController setViewControllers:@[nextViewController] direction:direction animated:YES completion:nil];
}

0 个答案:

没有答案