0

I have a PageViewController and three view controllers. I want to move to previous and next page with button click. I have three VCs and when I run, I'm in the second one, (main).

Ordered items: (in MainViewController class), and pageViewController is an instance of PageViewController class.

override func viewDidLoad() {
    super.viewDidLoad()
    orderedItems = self.pageViewController.orderedViewControllers
}

orderdViewControllers in PageViewController Class

private(set) lazy var orderedViewControllers: [UIViewController]? = {
    return [
        self.newViewController(id: "first"),
        self.newViewController(id: "main"),
        self.newViewController(id: "second")
    ]
}()

private func newViewController(id: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil) .
        instantiateViewController(withIdentifier: "\(id)")
}

this is my code for button taps: (in MainViewController class)

@IBAction func leftBtnPressed(_ sender: AnyObject) {
    self.pageViewController.setViewControllers([(orderedItems?.first)!], direction: .reverse, animated: true, completion: nil)
}

@IBAction func rightBtnPressed(_ sender: AnyObject) {
    self.pageViewController.setViewControllers([(orderedItems?.last)!], direction: .forward, animated: true, completion: nil)
}

but it fails, nothing happens. How can I do it?

The M7
  • 11
  • 5
  • Why are you using pageViewController for this purpose on first place. PageViewController is not meant for this type of functionality, you can use a collectionView for this functionality either. – Sanchit Kumar Singh Aug 05 '16 at 12:06
  • @SanchitKumarSingh i need that scrolling too. i want to move between pages with both scrolling and button. something like instagram's new update. you can go to stories or direct msg, either with button or with scroll. – The M7 Aug 05 '16 at 12:20

0 Answers0