UIPageviewcontroller滑动页面无法正常工作

时间:2016-01-23 13:22:07

标签: ios swift

我在登录后在我的应用中创建了一个基本的UIPageViewController。但是,我想修改分页并将初始viewcontroller设置为中间分页(页面A)like this

登录后,它只能显示初始但分页不起作用。我怎么解决这个问题?如果您需要更多信息,请发表评论。

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

1 个答案:

答案 0 :(得分:1)

在您的源代码中,您有

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

选择要开始的左页(索引0)。试试

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

从索引1开始=中间页=第A页。