2017-06-22 73 views
0

我想補充UIPageControllerUIViewviewController無法添加pageViewController到的UIView

這裏是我的代碼,但它不工作,

MyViewController含有一種觀點認爲這是_viewExpense有我想 加我viewcontroller

MyViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 

     PageViewControllerDashboard *pageViewController = [[UIStoryboard storyboardWithName:@"MoneyBag" bundle:nil] instantiateViewControllerWithIdentifier:@"PageViewControllerDashboard"]; 

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

PageViewControllerDashboard

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"]; 

    // Create page view controller 
    self.pageViewController.dataSource = self; 

    pageContentViewController *startingViewController = [self viewControllerAtIndex:0]; 
    NSArray *viewControllers = @[startingViewController]; 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    // Change the size of page view controller 
    self.pageViewController.view.frame = CGRectMake(10, 10, self.view.frame.size.width, self.view.frame.size.height - 100); 


    UIPageControl *pageControl = [UIPageControl appearance]; 
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; 
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor]; 
    pageControl.backgroundColor = [UIColor whiteColor]; 

} 

頁面視圖控制器數據源

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = ((pageContentViewController*) viewController).pageIndex; 

    if ((index == 0) || (index == NSNotFound)) { 
     return nil; 
    } 

    index--; 
    return [self viewControllerAtIndex:index]; 
} 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = ((pageContentViewController*) viewController).pageIndex; 

    if (index == NSNotFound) { 
     return nil; 
    } 

    index++; 
    if (index == [self.pageTitles count]) { 
     return nil; 
    } 
    return [self viewControllerAtIndex:index]; 
} 

- (pageContentViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) { 
     return nil; 
    } 

    // Create a new view controller and pass suitable data. 
    pageContentViewController *pageContentViewController = [[UIStoryboard storyboardWithName:@"MoneyBag" bundle:nil] instantiateViewControllerWithIdentifier:@"pageContentViewController"]; 
    pageContentViewController.titleText = self.pageTitles[index]; 
    pageContentViewController.pageIndex = index; 

    return pageContentViewController; 
} 

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

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

我在哪裏犯錯,請幫忙。先謝謝你。

回答

0

好了,我才注意到裏面PageViewControllerDashboard,在這條線的第一個錯誤:

self.pageViewController.dataSource = self; 

您的pageViewControllerdataSource屬性設置爲self。這裏您的pageViewController對象期望其所有datasource方法在PageViewControllerDashboard內聲明。相反,所有datasource方法都在PageViewControllerDataSource類中聲明。 所以,你應該做的是初始化PageViewControllerDataSource類的一個實例,像這樣:

PageViewControllerDataSource* pageViewControllerDataSource = [[PageViewControllerDataSource alloc] init] 

,一旦你糾正你UIPageController實例的datasource財產作出PageViewControllerDataSource數據源的pageViewController

的這種情況下,在PageViewControllerDataSource中聲明的所有數據源方法都將開始被調用,並且您的代碼也應該按照您的預期開始運行。