UIPageViewController, instantiating ViewControllers with Storyboard ID, Objective-C

混江龙づ霸主 提交于 2019-12-12 01:46:34

问题


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.

来源:https://stackoverflow.com/questions/29745656/uipageviewcontroller-instantiating-viewcontrollers-with-storyboard-id-objectiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!