Lyubomira Fileva Lyubomira Fileva - 1 month ago 24
iOS Question

Developing a Walkthrough Screen on Swift3

I am quite new in Swift and I am trying to develop a walkthrough screen for my application. I am following a book tutorial, but I get the following error and I can't continue.

I think the problem is in the different update of Swift. Would you please help me how to fix it?

self.pageViewController = (self.storyboard?.instantiateViewController(withIdentifier: "pageViewController") as! UIPageViewController!) as! UIPageViewControllerDataSource!

let startVC = self.viewControllerAtIndex(index: 0) as ContentViewController
let viewControllers = NSArray(object: startVC)

self.pageViewController.view.frame = CGRectMake (0,30,self.view.frame.width,self.view.frame.height-60)

self.addChildViewController(pageViewController as! UIViewController)
self.view.addSubview(pageViewController.view)

self.pageViewController.didMoveToParentViewController(self)


enter image description here

Answer

The pageViewController type is UIPageViewControllerDataSource. It is a protocol, not UIViewController.
You do not need to cast UIPageViewController to UIPageViewControllerDataSource. You need to implement a UIPageViewControllerDataSource and set pageViewController data source.

var pageViewController: UIPageViewController

self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "pageViewController") as! UIPageViewController
self.pageViewController.datasource = self

let startVC = self.viewControllerAtIndex(index: 0) as ContentViewController
let viewControllers = NSArray(object: startVC)

self.pageViewController.view.frame = CGRectMake (0,30,self.view.frame.width,self.view.frame.height-60)

self.addChildViewController(pageViewController as! UIViewController)
self.view.addSubview(pageViewController.view)

self.pageViewController.didMoveToParentViewController(self)