Ios 调用两次PageViewController委托函数
我正在与UIPageViewController合作,为我的应用程序进行产品介绍 我跟踪了这个链接 我正在做的是一项简单的任务,根据我得到的索引值,在滑动时更改我的“根VC”的背景颜色,但由于委托函数被调用了两次,我的索引值不正确,因此,我无法正确获得它,下面是我的代码Ios 调用两次PageViewController委托函数,ios,ios5,ios6,uipageviewcontroller,Ios,Ios5,Ios6,Uipageviewcontroller,我正在与UIPageViewController合作,为我的应用程序进行产品介绍 我跟踪了这个链接 我正在做的是一项简单的任务,根据我得到的索引值,在滑动时更改我的“根VC”的背景颜色,但由于委托函数被调用了两次,我的索引值不正确,因此,我无法正确获得它,下面是我的代码 #import "APPViewController.h" #import "APPChildViewController.h" @interface APPViewController () @end @implemen
#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;
}
请帮帮我,我不会错的
问候
Ranjit替换这两个方法并编译
- (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];
在寻找了很多之后
我收到:
- (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;
此函数用于为您提供PendingViewController数组和当前的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>
如果您有任何问题,请告诉我。我遇到了同样的问题,并最终找到了原因。 当页面视图控制器的转换样式设置为“page Curl”时,将调用委托方法两次 当转换样式设置为“页面卷曲”时,操作就像翻书一样。翻开一页,页码就会增加2。 将相同的概念应用于页面视图控制器,将为页面卷曲样式转换两个视图控制器:一个是您看到的视图控制器,另一个视图控制器位于卷曲视图的另一侧 将转换样式更改为“滚动”,就可以解决此问题 这可以通过属性检查器中的情节提要完成,如下所示: 通过将UIPageViewController.transitionStyle属性设置为UIPageViewControllerTransitionStyleScroll,还可以通过代码更改相同的设置
希望它能帮助你你到底想要一个带有UIView或uiviewcontrollers@VivekSehrawat,UIPageViewController的委托函数只能与VC一起使用。因此,我必须告诉我们,您想更改APPChildViewController或APPViewController的背景色吗?@NguyenThiThuyTrang,更改APPViewController@Ranjit :我有一个答案。并测试了它…你可以在上面尝试。委托函数没有被调用。你添加了吗?:self.pageController.delegate=self;这个函数在Simulator 6.0中可用
pendingViewController
是一个NSMutable视图控制器数组?你能添加它的代码吗?@Andronienn:是的,pendingViewController是NSMu视图控制器的tableArray。以及“您能添加其代码吗?”您的意思是什么?请详细解释。仅当您尝试从页面控制器更改页面而不是滑动手势时,这将不起作用。
if(index == 1)
{
self.view.backgroundColor = [UIColor redColor];
}
if(index == 2)
{
self.view.backgroundColor = [UIColor blueColor];
}
if(index == 3)
{
self.view.backgroundColor = [UIColor greenColor];
}