yoadle
yoadle

Reputation: 188

UIPageviewcontroller swipe page not working

I am creating a basic UIPageViewController in my apps after login. However, I would to modify the paging and set the initial viewcontroller as the middle one(page A) like this

After login it could only show the initial but the paging is not working. How can i solve this problem? Please leave a comment if you need anymore information.

class MainPageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {


var pageViewController: UIPageViewController!
let pages = ["SecondVC","MainVC","ThirdVC"]


//MARK: Page view controller datasource

func pageViewController(pageViewController: UIPageViewController,
    viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        if let index = pages.indexOf(viewController.restorationIdentifier!){
            if index > 0 {
                return viewControllerAtIndex(index - 1 )
            }
        }

        return nil
}

func pageViewController(pageViewController: UIPageViewController,
    viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

        if let index = pages.indexOf(viewController.restorationIdentifier!){

            if index < pages.count - 1 {
                return viewControllerAtIndex(index + 1)
            }
        }
        return nil
}

func viewControllerAtIndex(index: Int) -> UIViewController?{
    let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
    return vc
}


override func viewDidLoad() {
    super.viewDidLoad()

    if let vc = storyboard?.instantiateViewControllerWithIdentifier("StructurePageViewController"){
        self.addChildViewController(vc)
        self.view.addSubview(vc.view)

        pageViewController = vc as! UIPageViewController
        pageViewController.dataSource = self
        pageViewController.delegate = self

        pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil )
        pageViewController.didMoveToParentViewController(self)

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

enter image description here

Upvotes: 1

Views: 4148

Answers (1)

Gerd Castan
Gerd Castan

Reputation: 6849

in your source code you have the line

 pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil )

Which selects the left page to start (Index 0). Try

 pageViewController.setViewControllers([viewControllerAtIndex(1)!], direction: .Forward, animated: true, completion: nil )

to start at Index 1 = middle page = page A.

Upvotes: 1

Related Questions