Using the UIPageViewController in a storyboard

I want to use a UIPageViewController with an Xcode UIPageViewController . The only example I could find was this tutorial on how to implement the UIPageViewController using separate xib files.

However, in this tutorial, a UIPageViewController is created programmatically within a class that extends the UIViewController . This complicates the transition to how the storyboard interprets the UIPageViewController (which is itself an instance).

Any help on creating a functional UIPageViewController with an Xcode UIPageViewController would be greatly appreciated.

UPDATE: I was able to solve this problem by creating a new project in Xcode using the default pagecontroller template. He used the storyboard and was easy to follow.

+6
source share
3 answers

UPDATE: I was able to solve this problem by creating a new project in xcode using the default pagecontroller template. He used the storyboard and was easy to follow.

+8
source

Setting up an Xcode project as a PageViewController control is one way to achieve this, however, if you want to include a PageViewController in your existing storyboard, you can also do this.

If you drag the PageViewController Scene onto your storyboard and attach the cache, you can click on this PageViewController. However, it seems that there are some errors in the storyboard to correctly configure PageViewController, for example, you cannot connect a delegate and data source.

A simple way is to simply bind the delegate / data source in the 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 data source methods to be called properly if you of course want the data source and delegates to be PageViewController. Once this is configured, you will need to make sure that when viewing the view there is a view controller. You can do this with the setViewControllers method in the PageViewController class in your DidLoad view:

 - (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, if necessary, start calling your data source and delegate methods.

+7
source

There is an easy way to do this with Xcode 7. Have a ViewController that will be your data source and delegate it to the same storyboard scene, with your UIPageViewController built into the ContainerView. Then grab the UIPageViewController in the ReadySontroller ViewController method.

eg. 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 } } } 
0
source

Source: https://habr.com/ru/post/912981/


All Articles