Reputation: 2353
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.
Upvotes: 6
Views: 9763
Reputation: 7746
There's an easy way to do this with Xcode 7. Have the ViewController that will be your datasource and delegate in one Storyboard Scene, with your UIPageViewController embedded into a ContainerView. Then capture the UIPageViewController in your ViewController's prepareForSegue Method.
e.g. in Swift:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier where identifier == "yourPageVCIdentifier"{
if let pageController = segue.destinationViewController as? UIPageViewController {
pageController.dataSource = self
pageController.delegate = self
self.pageVC = pageController
}
}
}
Upvotes: 1
Reputation: 71
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.
Upvotes: 7
Reputation: 2353
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.
Upvotes: 8