2013-07-12 113 views
12

我正在使用UIPageViewController來爲我的應用程序進行產品導覽。PageViewController委託函數被調用兩次

我跟着這個鏈接http://www.appcoda.com/uipageviewcontroller-tutorial-intro/

我做的是改變我「根VC」上刷卡,基於索引值,我得到的backgound顏色的簡單的任務,但作爲委託函數被調用兩次,我的指數值是不正確的,因爲這一點,我不能得到它的權利,下面是我的代碼

#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

正是你想要用uiviews或uiviewcontrollers頁面控制 –

+0

@ VivekSehrawat,UIPageViewController的委託功能都只能在VC的工作是什麼。所以我不得不對我們VC – Ranjit

+0

你想改變APPChildViewController或APPViewController的背景顏色嗎? –

回答

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]; 
} 

讓我知道你是否有任何問題。

+0

代表函數沒有被調用 – Ranjit

+0

你補充嗎? :self.pageController.delegate = self; –

+0

對不起,我忘了它 – Ranjit

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];

+0

不行不行 – Ranjit

+0

還不行? –

+0

是的,仍然無法正常工作 – Ranjit

1

我面臨同樣的問題,並最終找出原因。 當頁面視圖控制器的Transition樣式設置爲「Page Curl」時,將委託方法調用兩次。

當過渡樣式設置爲「頁面捲曲」時,該操作就像在書中翻頁一樣。你翻一頁,頁碼增加2。 爲頁面視圖控制器應用相同的概念,2個視圖控制器轉換爲頁面捲曲樣式:一個是您看到的視圖控制器,另一個視圖控制器位於捲曲視圖的另一側。

更改過渡的風格,以「滾動」,你可以擺脫這個問題

這可以從情節串連圖板在屬性檢查器中進行的,如下所示: check image

您還可以更改由同通過將UIPageViewController.transitionStyle屬性設置爲UIPageViewControllerTransitionStyleScroll來進行編碼。

希望它可以幫助