无法将 pageViewController 添加到 UIView

Unable to add pageViewController to UIView

我想将 UIPageController 添加到 viewControllerUIView

这是我的代码,但它不起作用,

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

我哪里出错了,请帮忙。提前谢谢你。

嗯,我注意到的第一个错误是在 PageViewControllerDashboard 里面,在这一行:

self.pageViewController.dataSource = self;

您正在将 pageViewController 中的 dataSource 属性 设置为 self。此处您的 pageViewController 对象期望其所有 datasource 方法都在 PageViewControllerDashboard 内声明。相反,所有 datasource 方法都在 PageViewControllerDataSource class 中声明。 所以你应该做的是像这样初始化 class PageViewControllerDataSource 的实例:

PageViewControllerDataSource* pageViewControllerDataSource = [[PageViewControllerDataSource alloc] init]

并使 PageViewControllerDataSource 的这个实例成为 pageViewController

的数据源
PageViewControllerDataSource* pageViewControllerDataSource = [[PageViewControllerDataSource alloc] init];
self.pageViewController.dataSource = pageViewControllerDataSource;

一旦您更正 UIPageController 实例的 datasource 属性,在 PageViewControllerDataSource 内声明的所有数据源方法将开始被调用,您的代码也应该开始 运行如你所料。