UIPageViewController加载错误的页面

时间:2015-09-24 14:27:34

标签: ios objective-c

我在iOS应用程序中做了一个教程。但是我的PageViewController中有一个错误。

我的教程有5页。但我改变页面的第二个动作总是反复出现在上一页。

例如,出现page0,移至page1,然后当我想要出现page2时,再次出现page1。 这种情况发生在双方。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

 self.pageTexts = @[@"",intro1, intro2, intro3, intro4];

    self.index = 0;
    self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init];
    self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init];
    self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init];


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


    UIViewController* first = [self viewControllerUsed];
    NSArray *viewControllers = [NSArray arrayWithObject:first];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

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


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}




- (UIViewController *)viewControllerUsed{

    if (self.index == 0) {
        self.viewControllerPageContentOne=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentOne"];
        return self.viewControllerPageContentOne;
    }
    else if (self.index == 1)
    {
        self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"];
        self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
        return self.viewControllerPageContentTwo;
    }
    else if (self.index == 2)
    {
        self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"];
        self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
        return self.viewControllerPageContentTwo;
    }
    else if (self.index == 3)
    {
        self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"];
        self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index];
        return self.viewControllerPageContentTwo;
    }
    else if (self.index == 4)

    {
        self.viewControllerPageContentThree=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentThree"];
        self.viewControllerPageContentThree.titleText = self.pageTexts[self.index];
        return self.viewControllerPageContentThree;
    }
    else{
        return nil;
    }
}



#pragma mark - Page View Controller Data Source



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

    self.index--;
    if (self.index < 0) {
        self.index = [self.pageTexts count]-1;
    }
    return [self viewControllerUsed];


}



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

    self.index++;
    if (self.index > [self.pageTexts count]-1) {
        self.index = 0;
    }
    return [self viewControllerUsed];

}





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

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

我认为索引页面有错误,但我不知道。 感谢

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您正在为视图控制器创建类级别属性。你也是在viewDidLoad

中初始化它们
self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init];
self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init];
self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init];

我的建议是创建视图控制器并将它们保存在这样的数组中:

UIViewController *page0 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentOne"];

UIViewController *page1 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[1];

UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[2];

UIViewController *page3 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]
page3.titleText = self.pageTexts[3];

UIViewController *page4 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentThree"];

[self.viewControllers addObject:page0];
[self.viewControllers addObject:page1];
[self.viewControllers addObject:page2];
[self.viewControllers addObject:page3];
[self.viewControllers addObject:page4];

然后在viewControllerUsed中,只需从self.viewControllers返回viewControllers,如self.viewControllers[self.index]