2015-09-24 161 views
0

我在iOS應用程序中做了一個教程。但是我的PageViewController中有一個錯誤。UIPageViewController加載錯誤頁面

我的教程有5頁。但是我做的第二個轉變頁面總是反覆出現在前一頁。

例如,出現page0,移動到page1,然後當我想出現page2時,再次出現page1。 這發生在雙方。

這是我的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

self.pageTexts = @[@"",intro1, intro2, intro3, intro4]; 

    self.index = 0; 
    self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init]; 
    self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init]; 
    self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init]; 


    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"]; 
    self.pageViewController.dataSource = self; 


    UIViewController* first = [self viewControllerUsed]; 
    NSArray *viewControllers = [NSArray arrayWithObject:first]; 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    [self addChildViewController:self.pageViewController]; 
    [self.view addSubview:self.pageViewController.view]; 
    [self.pageViewController didMoveToParentViewController:self]; 


} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 

} 




- (UIViewController *)viewControllerUsed{ 

    if (self.index == 0) { 
     self.viewControllerPageContentOne=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentOne"]; 
     return self.viewControllerPageContentOne; 
    } 
    else if (self.index == 1) 
    { 
     self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]; 
     self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index]; 
     return self.viewControllerPageContentTwo; 
    } 
    else if (self.index == 2) 
    { 
     self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]; 
     self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index]; 
     return self.viewControllerPageContentTwo; 
    } 
    else if (self.index == 3) 
    { 
     self.viewControllerPageContentTwo=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"]; 
     self.viewControllerPageContentTwo.titleText = self.pageTexts[self.index]; 
     return self.viewControllerPageContentTwo; 
    } 
    else if (self.index == 4) 

    { 
     self.viewControllerPageContentThree=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentThree"]; 
     self.viewControllerPageContentThree.titleText = self.pageTexts[self.index]; 
     return self.viewControllerPageContentThree; 
    } 
    else{ 
     return nil; 
    } 
} 



#pragma mark - Page View Controller Data Source 



- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 

    self.index--; 
    if (self.index < 0) { 
     self.index = [self.pageTexts count]-1; 
    } 
    return [self viewControllerUsed]; 


} 



- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 

    self.index++; 
    if (self.index > [self.pageTexts count]-1) { 
     self.index = 0; 
    } 
    return [self viewControllerUsed]; 

} 





- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController 
{ 
    return [self.pageTexts count]; 
} 

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController 
{ 
    return 0; 
} 

我假設有在索引頁面的錯誤,但我不知道它。 謝謝

+0

你有沒有注意到在你的'viewControllerUsed'索引1,2和3中,你返回的視圖控制器對象具有不同的標題。這是預期的嗎? – Abhinav

+0

您的viewControllerUsed方法爲索引1-3返回相同的視圖控制器,這可能會解釋您所看到的內容。嘗試改變它以返回不同的視圖控制器。 –

+0

是的,這個想法是,與指數1,2,3的屏幕是相同的看法,但具有不同的標題或文字 – user3086708

回答

0

我認爲你的問題是,你正在爲視圖控制器創建類級屬性。而那也是你初始化它們viewDidLoad

self.viewControllerPageContentOne = [[ViewControllerPageContentOne alloc] init]; 
self.viewControllerPageContentTwo = [[ViewControllerPageContentTwo alloc] init]; 
self.viewControllerPageContentThree = [[ViewControllerPageContentThree alloc] init]; 

我的建議是創建視圖控制器,並將它們保存在一個這樣的數組:從self.viewControllers

UIViewController *page0 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentOne"]; 

UIViewController *page1 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"] 
page3.titleText = self.pageTexts[1]; 

UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"] 
page3.titleText = self.pageTexts[2]; 

UIViewController *page3 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentTwo"] 
page3.titleText = self.pageTexts[3]; 

UIViewController *page4 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerPageContentThree"]; 

[self.viewControllers addObject:page0]; 
[self.viewControllers addObject:page1]; 
[self.viewControllers addObject:page2]; 
[self.viewControllers addObject:page3]; 
[self.viewControllers addObject:page4]; 

,然後在viewControllerUsed簡單地返回viewControllers self.viewControllers[self.index]