Normally I would've done the following to create a view controller and set its dataSource
using Objective-C:
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the data model
_pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark Favorite Tip", @"Free Regular Update"];
_pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
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];
}
Now I would like to convert it to Swift. This is my attempt so far:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Create page view controller
self.pageViewController = self.storyboard.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
self.pageViewController.dataSource = self;
var startingViewController = PageContentViewController(nibName: nil, bundle: nil)
startingViewController = self.viewControllerAtIndex(0)
var viewControllers: NSArray = [startingViewController];
self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection, animated: 0, completion: nil)
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
I'm not sure why it's throwing an error for:
self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection, animated: 0, completion: nil)
Error: Expected member name or constructor call after type name.
The type of the direction:
parameter is UIPageViewControllerNavigationDirection
, therefore the argument must be one of the possible values, e.g. UIPageViewControllerNavigationDirection.Forward
(which can be shortened to .Forward
due to the automatic type inference).
And the type of the animated:
parameter is Bool
, therefore the argument must
be true
or false
, and not 0
.
Together:
self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: nil)
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