Bansal
Bansal

Reputation: 67

Error in pageviewcontroller cannot convert [AnyObject] to expected arguments

I am new to iOS developer and doing pageViewController in which i take four labels and these four label while be shown on each screen respectively.But I am getting the error while taking this in array like in this code in have shown

var pageviewcontroller:UIPageViewController!
var label:NSArray!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.label = NSArray(objects: "1", "2", "3", "4")

    self.pageviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

    self.pageviewcontroller.dataSource = self

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

self.pageviewcontroller.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

    self.pageviewcontroller.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 50)

    self.addChildViewController(self.pageviewcontroller)
    self.view.addSubview(self.pageviewcontroller.view)
    self.pageviewcontroller.didMoveToParentViewController(self)
}

In this line i am getting the error of any object cannot shown to be expected arguments

Upvotes: 1

Views: 45

Answers (1)

Andrey Gordeev
Andrey Gordeev

Reputation: 32449

That's because you're passing an array of AnyObjects to UIPageViewController's setViewControllers(_:direction:animated:completion:) method. It expects an array of UIViewControllers. Change that piece of code to this:

var startVC = self.viewControllerAtIndex(0) as ContentViewController
self.pageviewcontroller.setViewControllers([startVC], direction: .Forward, animated: true, completion: nil)

Also you shouldn't use NSArrays in Swift. Use standard Swift arrays instead.

Upvotes: 1

Related Questions