UIPageViewController, instantiating ViewController

2019-09-08 14:57发布

问题:

I am trying to implement UIPageViewController and I believe that I am in the last stage. At first the UIPageViewController only used one VC to scroll to a "different" page. Now i want it to scroll between different ViewControllers. This is the code i am using:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.vcIdentifiers = @[@"PageContentViewController", @"PageContentViewController1"];


    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

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

    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

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

}

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


    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    pageContentViewController.titleText = self.pageTitles[index];
    pageContentViewController.pageIndex = index;

    return pageContentViewController;
}

I belive it is in the viewControllerAtIndex i need to make some changes. I thought of doing this:

PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.vcIdentifiers[index]];

But then it still would initialise the PageContentViewController. So my problem is i don't know how to initialise the different ViewControllers.