問題描述
我正在使用 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 委托函數調用了兩次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!