简体   繁体   中英

IOS - How to create a UIPageViewController with multiple ViewController from different XIB files

My main controller has a UIPageViewController. I want to swipe across differently structured views, so I can't use the same controller. Also, I don't want to pollute the main storyboard. I want to have different XIB files with custom controllers. So far so good.

I want to be able to provide the index to the UIPageViewController.

This answer shows how to do it with multiple view controllers but they are all within the storyboard.

I have tried the same approach. I created my XIBs with corresponding ViewControllers. In order to provide the index to the PageViewController, I tried setting the RestorationId to the view to access later in the code. In the PageViewController, I build the controller like this:

func viewControllerAtIndex(index: Int) -> UIViewController {
    let vc = UIViewController(nibName: controllersNames[index], bundle: nil)
    ...
}

But if I do

vc.restorationIdentifier

I get nil..

So I can't seem to find a way to bind the controller to the index I need to provide the UIPageViewController.

Please help.

You need to create like a main view controller and in this view controller you initialise all VC you need.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        // initialize controllers
        self.controllers = [[NSMutableArray alloc]
                            initWithObjects:
                            [[Document1ViewController alloc] initWithNibName:@"Document1ViewController" bundle:nil],
                            [[Document2ViewController alloc] initWithNibName:@"Document2ViewController" bundle:nil],
                            [[Document3ViewController alloc] initWithNibName:@"Document3ViewController" bundle:nil],
                            nil];
    }
    return self;
}

Then you need to create a scrollview with pagination with the number of VC you want.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    for (NSUInteger i =0; i < [self.controllers count]; i++) {
        [self loadScrollViewWithPage:i];
    }

    self.pageControl.currentPage = 0;
    _page = 0;
    [self.pageControl setNumberOfPages:[self.controllers count]];

    UIViewController *viewController = [self.controllers objectAtIndex:self.pageControl.currentPage];
    if (viewController.view.superview != nil) {
        [viewController viewWillAppear:animated];
    }

    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.controllers count], scrollView.frame.size.height);
}

When you scroll you need to change the VC order in the scrollView and is possible because you have a array with the references of all VC's.

You can see one example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM