I am creating a page view application. I have written code for the ViewController:
import UIKit
class ViewController: UIViewController {
var pageViewController : UIPageViewController!
var pageTitles : NSArray!
var pageImages : NSArray!
@IBAction func restartAction(sender: AnyObject) {}
override func viewDidLoad() {
super.viewDidLoad()
self.pageTitles = NSArray(objects: "This Is Page One!", "This Is Page Two!")
self.pageImages = NSArray(objects: "page1", "page2")
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
self.pageViewController.dataSource = self // <–– Error
let startVC = self.viewControllerAtIndex(0) as ContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.height - 60)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewControllerAtIndex(index: Int) -> ContentViewController {
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return ContentViewController()
}
let vc : ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
vc.imageFile = self.pageImages[index] as! String
vc.titleText = self.pageTitles[index] as! String
vc.pageIndex = index
return vc
}
func pageViewController(pageViewController: UIPageViewController, viewcontrollerBeforeViewController viewController: UIViewController) ->UIViewController? {
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if index == 0 || index == NSNotFound {
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewcontrollerAfterViewController viewController: UIViewController) ->UIViewController? {
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if index == NSNotFound {
return nil
}
index++
if index == self.pageTitles.count {
return nil
}
return self.viewControllerAtIndex(index)
}
}
But I am getting an error on this line self.pageViewController.dataSource = self
. The error is:
Cannot assign to property: 'self' is immutable
I am unsure of why self is immutable and how to fix it. If someone could please help, thanks in advance.
In swift you'll need to call self.pageViewController.setDelegate(self)
Delegates have been declared as methods, not properties in some cases