我正在使用UIPageViewController,为我的应用程序进行产品导览。
我点了这个链接http://www.appcoda.com/uipageviewcontroller-tutorial-intro/
我正在做的是根据我获得的索引值在滑动时更改我的“root 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;
}
请帮助我,我没有得到我错的地方
此致 兰吉特
答案 0 :(得分:10)
寻找了很多。
我收到了:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
2个函数用于获取当前pageViewController后面或前面的pageViewController。
我认为很难获得当前的pageViewController
我的建议:
在UIPageViewControllerDelegate中,它有一个函数:
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;
这个函数给你一个pendingViewControllers数组和这个当前的pageViewController数组。 所以你可以这样实现:
- (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];
}
}
}
在viewDidLoad中,添加:
self.pageController.delegate = self;
self.view.backgroundColor = [UIColor redColor]; //set first background.
在'APPViewController.h'中你确定添加:
@interface APPViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
请记住:删除此代码(在'viewControllerAtIndex'函数中)
if(index == 1)
{
self.view.backgroundColor = [UIColor redColor];
}
if(index == 2)
{
self.view.backgroundColor = [UIColor blueColor];
}
if(index == 3)
{
self.view.backgroundColor = [UIColor greenColor];
}
如果您有任何问题,请告诉我。
答案 1 :(得分: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];
答案 2 :(得分:1)
我遇到了同样的问题,最终找到了原因。 当页面视图控制器的“过渡”样式设置为“页面卷曲”时,将调用委托方法两次。
当过渡样式设置为“页面卷曲”时,操作就像翻书中的页面一样。您翻页,页码增加2。 对页面视图控制器应用相同的概念,为页面卷曲样式转换了2个视图控制器:一个是您看到的视图控制器,另一个视图控制器位于卷曲视图的另一侧。
将过渡样式更改为“滚动”,您可以摆脱问题
这可以从属性检查器中的storyboard完成,如下所示: check image
您也可以通过将UIPageViewController.transitionStyle属性设置为UIPageViewControllerTransitionStyleScroll来更改代码。
希望有所帮助