UIPageViewController 索引无法正常工作

Posted

技术标签:

【中文标题】UIPageViewController 索引无法正常工作【英文标题】:UIPageViewController index is not working properly 【发布时间】:2015-06-11 07:16:21 【问题描述】:

基本上,我正在制作一个使用 UIPageViewController 的页面应用程序。当用户滚动页面时,他们可以看到不同的内容。现在的问题是,当我滚动页面时,第一页和第二页可以正常工作,并且索引是正确的。但是当我滚动到第三页时,索引保持在“1”,之后不会改变。而另一页保持第二页的外观,其索引为“1”。

我正在学习教程:http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

我的代码:

查看控制器.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

查看 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

页面内容视图控制器.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

页面内容视图控制器.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】:

viewControllerAtIndex:中,需要设置pageContentViewControllerpageIndex属性的值:

pageContentViewController.pageIndex = index;

【讨论】:

以上是关于UIPageViewController 索引无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

页面更改时iOS UIPageViewController索引不增加

UIPageViewController 可以返回错误的索引

避免用户使用 UIPageViewController 滚动到索引 -1 和 2

如何跳回 UIPageviewController 中页面的第一个索引

uitableviewcontroller 编辑索引处的行不适用于 uipageviewcontroller

UICollectionViewController 在 UIPageViewController 中时无法滚动