我正在使用 UIPageViewController 为我的应用程序制作产品导览.
I am working with UIPageViewController , to make a product tour for my application.
我点击了这个链接 http://www.appcoda.com/uipageviewcontroller-tutorial-intro/
我正在做的是根据我得到的索引值在滑动时更改 root VC" 的背景颜色的简单任务,但是由于委托函数被调用两次,我的索引值是不正确,因此,我无法正确处理,下面是我的代码
I am doing is simple task of changing backgound color of my "root VC" on swipe, based on the index value I get, but as the delegate functions are called twice, my index value is not correct and because of that, I am not able to get it right, below is my code
#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;
}
请帮帮我,我不知道哪里出错了
Please help me out, I am not getting where I am going wrong
问候兰吉特
找了很多.
我收到了:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
2 个函数用于获取当前 pageViewController 后面或前面的 pageViewController.
2 functions use to get pageViewController behind or in front of current pageViewController.
我认为获取当前 pageViewController 很困难
I thinks it's difficult to get current pageViewController
我的建议:
在 UIPageViewControllerDelegate 中,它有一个功能:
In UIPageViewControllerDelegate, it have a function :
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;
这个函数给你一个pendingViewControllers 数组和当前pageViewController 数组.所以你可以这样实现:
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];
}
}
}
在 viewDidLoad 中,添加:
In viewDidLoad, you add :
self.pageController.delegate = self;
self.view.backgroundColor = [UIColor redColor]; //set first background.
在 'APPViewController.h' 中你确定添加:
In 'APPViewController.h' you sure add:
@interface APPViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
记住:删除这段代码(在'viewControllerAtIndex'函数中)
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.
这篇关于PageViewController 委托函数调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!