UIPageViewController index is not working properly

671 views Asked by At

Basically, I am making a page app, which use UIPageViewController. When the user scrolls the page, they can see different content. The problem now is, when I scroll the page, the first and second page works properly, and the index is correct. But when I scroll to the third page, the index keeps at "1", and don't change afterwards. And the other page keeps the appearance of the second page, whose index is "1".

I was following the tutorial: http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

My code:

View Controller.h

#import <UIKit/UIKit.h>
#import "PageContentViewController.h"
#import "PageViewController.h"

@interface ViewController : UIViewController<UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (strong, nonatomic) PageViewController *pageViewController;
@property (strong, nonatomic) NSArray *pageTitles;


@end

View Controller.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.pageTitles = @[@"First Page", @"Second Page", @"Third Page"];

    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    if ([self.pageTitles count])
    {
        PageContentViewController *startingViewController = [self viewControllerAtIndex: 0];

        NSArray *viewControllers = @[startingViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

        self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self addChildViewController:self.pageViewController];
        [self.view addSubview:self.pageViewController.view];
        [self.pageViewController didMoveToParentViewController:self];
    }
}

- (PageContentViewController *) viewControllerAtIndex: (NSUInteger)index{
    if (index < [self.pageTitles count])
    {
        PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];

        pageContentViewController.titleText = self.pageTitles[index];

        NSLog(@"%li", index);

        UIColor *backgroundColor;
        switch (index) {
            case 0:
                backgroundColor = [UIColor greenColor];
                break;

            case 1:
                backgroundColor = [UIColor blueColor];
                break;

            case 2:
                backgroundColor = [UIColor purpleColor];
                break;

            default:
                break;
        }

        pageContentViewController.backgroundColor = backgroundColor;
        return pageContentViewController;
    }

    return nil;
}

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


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

    index++;

    if (index == [self.pageTitles count])
    {
        return nil;
    }

    return [self viewControllerAtIndex:index];
}

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

    index--;

    return [self viewControllerAtIndex:index];
}

- (NSInteger) presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return [self.pageTitles count];
}

- (NSInteger) presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Page Content View Controller.h

#import <UIKit/UIKit.h>

@interface PageContentViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;

@property (nonatomic) NSUInteger pageIndex;

@property (strong, nonatomic) NSString *titleText;
@property (strong, nonatomic) UIColor *backgroundColor;

@end

Page Content View Controller.m

#import "PageContentViewController.h"

@interface PageContentViewController ()

@end

@implementation PageContentViewController

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

    self.titleLable.text = self.titleText;
    [self.backgroundView setBackgroundColor:self.backgroundColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
1

There are 1 answers

0
pbasdf On

In viewControllerAtIndex:, you need to set the value of pageIndex property of the pageContentViewController:

pageContentViewController.pageIndex = index;