1

I've created a PageViewController, which i'm trying to make into a quiz where when you press a button it will slide to the next view. I can't seem to get this to work. so far i've added a tableView which contain a button, which i would like to make this gesture with. Beside this i would like to disable the scroll gesture, so the user can't manually scroll. Is this possible or would it make more sense to use a scrollView for this?

viewDidLoad

    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("QuizPageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self

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

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)

PageViewController delegate

func viewControllerAtIndex(index: Int) -> QuestionViewController
{
    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return QuestionViewController()
    }

    let vc: QuestionViewController = self.storyboard?.instantiateViewControllerWithIdentifier("QuestionViewController") as! QuestionViewController
    vc.questionText = self.pageTitles[index] as! String
    vc.pageIndex = index
    vc.pageCount = self.pageTitles.count
    return vc
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
    let vc = viewController as! QuestionViewController
    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! QuestionViewController
    var index = vc.pageIndex as Int
    print(index)
    if (index == NSNotFound)
    {
        return nil
    }
    index++
    if (index == self.pageTitles.count)
    {
        return nil
    }
    return self.viewControllerAtIndex(index)
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return 0
}
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • It appears you're asking 2 questions. How to disable scrolling is answered here http://stackoverflow.com/questions/13373531/uipageviewcontroller-disable-scrolling. And how to scroll programmatically is answered here http://stackoverflow.com/questions/7208871/is-it-possible-to-turn-page-programmatically-in-uipageviewcontroller. – Shamas S Oct 05 '15 at 08:17
  • This is all Objective-c! beside this these answes all seem to be work arounds and not ideal, thats why im asking also what is best practice to achieve this. – Peter Pik Oct 05 '15 at 08:19
  • `UIPageViewController` doesn't come with the functionality you require. Hence these "workarounds" are adopted. If you are having trouble translating these answers to Swift, post another question, or update this one. – Shamas S Oct 05 '15 at 08:48
  • First of all The scrollEnabled function is not working in 2.0. Secondly my case is different since i have dynamic pages where in the other question they have static views! so two different scenarios – Peter Pik Oct 05 '15 at 10:18

0 Answers0