2013-07-12 12 views
12

Pracuję z UIPageViewController, aby utworzyć wycieczkę produktu dla mojej aplikacji.Funkcje delegatów PageViewController wywoływane dwukrotnie

Śledziłem ten link http://www.appcoda.com/uipageviewcontroller-tutorial-intro/

robie jest prostym zadaniem zmieniając kolor backgound mojego „root VC” na bezstykowa, na podstawie wartości indeksu dostaję, ale jako funkcje delegata nazywane są dwa razy moja wartość indeksu nie jest poprawna i dlatego nie jestem w stanie uzyskać to prawo, poniżej jest mój kod

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

Proszę mi pomóc, ja nie dostaję gdzie wezwę niewłaściwy

Pozdrowienia Ranjit

+0

co dokładnie chcesz kontrolę ze strony UIViews lub uiviewcontrollers –

+0

@ VivekSehrawat funkcje delegata UIPageViewController pracować tylko z VC. Więc muszę do nas VC – Ranjit

+0

Czy chcesz zmienić kolor tła APPChildViewController lub APPViewController? –

Odpowiedz

10

Po patrząc za dużo.

I otrzymujemy:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController; 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController; 

2 funkcje użyć, aby uzyskać pageViewController za lub przed bieżącym pageViewController.

I uważa, że ​​to trudne do uzyskania aktualnych pageViewController

Moja sugestia:

W UIPageViewControllerDelegate, to posiada funkcję:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers; 

tej funkcji, aby dać Ci tablicę pendingViewControllers i to jest prąd pageViewController szyk. Więc można wdrożyć tak:

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


    } 
} 

W viewDidLoad należy dodać:

self.pageController.delegate = self; 

    self.view.backgroundColor = [UIColor redColor]; //set first background. 

W 'APPViewController.h' na pewno dodać:

@interface APPViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate> 

Pamiętaj: usunąć ten kod (w funkcji '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]; 
} 

Daj mi znać, jeśli masz jakieś pytania.

+0

funkcja delegata nie jest wywoływana – Ranjit

+0

czy chcesz dodać? : self.pageController.delegate = self; –

+0

Przepraszam, zapomniałem – Ranjit

1

Wymienić tych dwóch metod i skompilować,

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

} 

i dodać to w końcu - (void)viewDidLoad {

self.view.backgroundColor = [UIColor redColor];

+0

Nie jego nie działa – Ranjit

+0

nadal nie działa ???? –

+0

Tak, nadal nie działa – Ranjit

1

Zmierzyłem się z tym samym problemem iw końcu zrozumiałem przyczynę. Metody pełnomocników są wywoływane dwukrotnie, gdy styl przejścia dla kontrolera widoku strony jest ustawiony na "Zwinięcie strony".

Gdy styl przejścia jest ustawiony na "Zawinięcie strony", działanie jest podobne do obracania strony w książce. Obracasz jedną stronę, a numer strony zwiększa się o 2. Stosując tę ​​samą koncepcję dla kontrolera widoku strony, 2 kontrolery widoku są przekształcane na styl Curl strony: jeden to kontroler widoku, a drugi kontroler widoku znajduje się po drugiej stronie zawiniętego widoku.

zmienić styl przejścia na „Scroll” i można pozbyć się problemu

To można zrobić z ujęć w Inspektorze atrybutu następująco: check image

Można także zmienić samo przez kod, ustawiając właściwość UIPageViewController.transitionStyle na UIPageViewControllerTransitionStyleScroll.

Nadzieja pomaga

Powiązane problemy