无法将 pageViewController 添加到 UIView

Posted

技术标签:

【中文标题】无法将 pageViewController 添加到 UIView【英文标题】:Unable to add pageViewController to UIView 【发布时间】:2017-06-22 05:56:05 【问题描述】:

我想将UIPageController 添加到UIViewviewController

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

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;

我在哪里犯错请帮忙。提前谢谢你。

【问题讨论】:

【参考方案1】:

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

self.pageViewController.dataSource = self;

您将pageViewControllerdataSource 属性设置为self。这里你的pageViewController 对象期望它的所有datasource 方法都在PageViewControllerDashboard 中声明。相反,所有datasource 方法都在PageViewControllerDataSource 类中声明。 所以你应该做的是像这样初始化PageViewControllerDataSource 类的一个实例:

PageViewControllerDataSource* pageViewControllerDataSource = [[PageViewControllerDataSource alloc] init]

并将PageViewControllerDataSource 数据源的这个实例设为pageViewController

PageViewControllerDataSource* pageViewControllerDataSource = [[PageViewControllerDataSource alloc] init];
self.pageViewController.dataSource = pageViewControllerDataSource;

一旦您更正了 UIPageController 实例的 datasource 属性,在 PageViewControllerDataSource 中声明的所有数据源方法都将开始被调用,并且您的代码也应该开始按预期运行。

【讨论】:

以上是关于无法将 pageViewController 添加到 UIView的主要内容,如果未能解决你的问题,请参考以下文章

将数据从 TableView 传递到 PageViewController 的包含视图

PageViewController 的自定义页面控件

如何在 collectionview 中添加 PageviewController

pageViewController 的视差效果

将按钮水平添加到页面控件

iOS/Swift/Storyboard:仅使用屏幕的 *part* 添加 PageViewController?