I'm looking to use the UIPageViewController
with Xcode's storyboard. The only example I could find was this tutorial on how to implement UIPageViewcontroller
using separate xib files.
However in this tutorial the UIPageViewController
is instantiated programmatically within a class that extends UIViewController
. This makes it complicated to translate in to how the storyboard interprets the UIPageViewcontroller
(which is as an instance on its own).
Any help on how to create a functioning UIPageViewController
with Xcode's storyboard will be much appreciated.
UPDATE: I managed to resolve this issue by making a new project in Xcode using the default pagecontroller template. It used the storyboard and was easy to follow.
Drag # of View Controllers for # of pages you want onto storyboard Set a name for the storyboard ID and restoration ID Code 4. Create UIPageViewController class and inherit from UIPageViewController, UIPageViewControllerDatasource, and UIPageViewControllerDelegate 5. Add the required methods for the Datasource Storyboard 6.
Create a function to retrieve view controllers (pages of your application) Create a lazily instantiated array: Each index is populated by calling the function we just created In viewDidLoad: Set the first view controller of the UIPageViewController from the first index of the array.
As usual, you should find a default view controller generated by Xcode. Leave it as it is. Drag a Page View Controller from the Object Library into the storyboard. Then add another View Controller and put it in the same storyboard.
The UIPageViewController class highlights the distinction between a view controller and a container controller. The container controller is used to contain and manage multiple view controllers shown in the app, as well as, controlling the way one view controller switches to another.
UPDATE: I managed to resolve this issue by making a new project in xcode using the default pagecontroller template. It used the storyboard and was easy to follow.
Setting up the Xcode Project to be a PageViewController is one way of accomplishing this, however if you'd like to include a PageViewController within an already existing storyboard you can do this as well.
If you drag and drop a PageViewController Scene onto your storyboard and wire up a segue, you can push to that PageViewController. However there appears to be some bugs in the storyboard for setting up the PageViewController properly, for instance you cannot wire up the delegate and datasource.
An easy way around this is to simply wire the delegate/datasource up in your init method:
- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
self.delegate = self;
self.dataSource = self;
}
return self;
}
This will cause your delegate and datasource methods to be called properly, assuming of course you want the datasource and delegates to be the PageViewController. Once this is set up you will need to ensure that there is a view controller when the view is loaded. You can do this with the setViewControllers method on PageViewController class in your viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setViewControllers:@[sweetViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
NSLog(@"Hooray I set my initial viewcontroller for my page view controller");
}];
}
When the PageViewController is created, it will start with your sweetViewController, and then begin calling your datasource and delegate methods as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With