14

我正在使用 UIPageViewController 为我的应用程序制作产品导览。

我点击了这个链接http://www.appcoda.com/uipageviewcontroller-tutorial-intro/

我正在做的是根据我得到的索引值在滑动时更改“根 VC”的背景颜色的简单任务,但是由于委托函数被调用两次,我的索引值不正确,因此,我不是能够做对,下面是我的代码

#import "APPViewController.h"
#import "APPChildViewController.h"

@interface APPViewController ()

@end

@implementation APPViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:CGRectMake(0, 0, 320, 500)];

    APPChildViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

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

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

- (APPChildViewController *)viewControllerAtIndex:(NSUInteger)index {

    APPChildViewController *childViewController = [[APPChildViewController alloc] initWithNibName:@"APPChildViewController" bundle:nil];
    childViewController.index = index;
    childViewController.view.backgroundColor = [UIColor clearColor];

    if(index == 0)
    {
          self.view.backgroundColor = [UIColor redColor];
     }

    if(index == 1)
    {
          self.view.backgroundColor = [UIColor blueColor];
     }

    if(index == 2)
    {
          self.view.backgroundColor = [UIColor greenColor];
     }


    return childViewController;

}

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

    NSUInteger index = [(APPChildViewController *)viewController index];

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

    // Decrease the index by 1 to return
    index--;

   return [self viewControllerAtIndex:index];

}

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

    NSUInteger index = [(APPChildViewController *)viewController index];

    index++;

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

   return [self viewControllerAtIndex:index];

}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    // The number of items reflected in the page indicator.
    return 3;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    // The selected item reflected in the page indicator.
    return 0;
}

请帮帮我,我没有找到我要去的地方

问候兰吉特

4

3 回答 3

10

After looking for a lot.

I receive that:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

2 functions use to get pageViewController behind or in front of current pageViewController.

I thinks it's difficult to get current pageViewController

My suggestion:

In UIPageViewControllerDelegate, it have a function :

 - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;

This function to give you a pendingViewControllers array and this's current pageViewController array. So you can implement like that :

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{


if([pendingViewControllers count]>0)
  {
     NSUInteger index =[(APPChildViewController*)[pendingViewControllers objectAtIndex:0] index];

    if(index == 0)
    {
        self.view.backgroundColor = [UIColor redColor];
    }

    if(index == 1)
    {
        self.view.backgroundColor = [UIColor blueColor];
    }

    if(index == 2)
    {
        self.view.backgroundColor = [UIColor greenColor];
    }


  }
}

In viewDidLoad, you add :

    self.pageController.delegate = self;

    self.view.backgroundColor = [UIColor redColor]; //set first background.

In 'APPViewController.h' you sure add:

@interface APPViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

Remember : remove this code (in 'viewControllerAtIndex' function)

if(index == 1)
{
    self.view.backgroundColor = [UIColor redColor];
}

if(index == 2)
{
    self.view.backgroundColor = [UIColor blueColor];
}

if(index == 3)
{
    self.view.backgroundColor = [UIColor greenColor];
}

Let's me know if you have any questions.

于 2013-07-12T08:27:07.830 回答
2

我遇到了同样的问题,终于找到了原因。当页面视图控制器的转换样式设置为“页面卷曲”时,会调用两次委托方法。

当过渡样式设置为“Page Curl”时,动作就像翻书一样。您翻一页,页码增加 2。对页面视图控制器应用相同的概念,2 个视图控制器转换为 Page Curl 样式:一个是您看到的视图控制器,另一个视图控制器在另一侧的卷曲视图。

将过渡样式更改为“滚动”即可摆脱问题

这可以从属性检查器中的情节提要中完成,如下所示:

检查图像

您还可以通过将 UIPageViewController.transitionStyle 属性设置为 UIPageViewControllerTransitionStyleScroll 来通过代码进行更改。

希望能帮助到你

于 2017-07-26T06:56:16.207 回答
1

替换这两个方法并编译,

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

    NSUInteger index = [(APPChildViewController *)viewController index];
    if (index == 0)
    {
        self. view.backgroundColor = [UIColor redColor];
        return nil;
    }

    if(index == 1)
    {
        self.view.backgroundColor = [UIColor blueColor];
    }

    if(index == 2)
    {
        self.view.backgroundColor = [UIColor greenColor];
    }
    if(index == 3)
    {
        self.view.backgroundColor = [UIColor brownColor];
    }
    /*if(index == 4)
    {
        self.view.backgroundColor = [UIColor whiteColor];
    }*/

    // Decrease the index by 1 to return
    index--;

    return [self viewControllerAtIndex:index];

}

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

    NSUInteger index = [(APPChildViewController *)viewController index];
    /*if(index == 0)
    {
        self.view.backgroundColor = [UIColor redColor];
    }*/
    if(index == 1)
    {
        self.view.backgroundColor = [UIColor blueColor];
    }

    if(index == 2)
    {
        self.view.backgroundColor = [UIColor greenColor];
    }
    if(index == 3)
    {
        self.view.backgroundColor = [UIColor brownColor];
    }
    if(index == 4)
    {
        self.view.backgroundColor = [UIColor whiteColor];
        return nil;
    }

    /*if (index == 5) {
        return nil;
    }*/
    index++;

    return [self viewControllerAtIndex:index];

}

并将其添加到末尾- (void)viewDidLoad {

self.view.backgroundColor = [UIColor redColor];

于 2013-07-12T06:58:32.290 回答