视图控制器与按钮之间的转换(UIPageViewController项)

时间:2017-03-01 20:24:44

标签: swift uipageviewcontroller

我有2个UIViewControllers作为UIPageViewController的项目。它适用于幻灯片,但我想通过按钮的操作从pageOne移动到pageTwo。我怎么能这样做?

这是我的代码和故事板预览:

Pager.swift的内容:

nasm -f elf ppp.asm; ld -m elf_i386 -s -o ppp ppp.o; ppp

PageOne.swift的内容:

import UIKit

class Pager: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource, UIScrollViewDelegate
{
var viewsArray: [UIViewController] = []

var pageOne: UIViewController = UIViewController()
var pageTwo: UIViewController = UIViewController()

var currentIndex = 0
var previousIndex = 0
var nextIndex = 0

override func viewDidLoad()
{
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    pageOne = (storyboard?.instantiateViewController(withIdentifier: "pageOne"))!
    pageTwo = (storyboard?.instantiateViewController(withIdentifier: "pageTwo"))!

    viewsArray.append(pageOne)
    viewsArray.append(pageTwo)

    setViewControllers([pageOne], direction: .forward, animated: true, completion: nil)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
    currentIndex = viewsArray.index(of: viewController)!
    if currentIndex == 1
    {
        return nil
    }
    else
    {
        nextIndex = abs((currentIndex + 1) % viewsArray.count)
        return viewsArray[nextIndex]
    }
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
    currentIndex = viewsArray.index(of: viewController)!
    if currentIndex == 0
    {
        return nil
    }
    else
    {
        previousIndex = abs((currentIndex - 1) % viewsArray.count)
        return viewsArray[previousIndex]
    }
}


func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return viewsArray.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return 0
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在下面写一行

pageController?.setViewControllers([YOUR_VIEW_CONTROLLER], direction: direction, animated: true, completion: nil)

根据你所在的控制器正确或反向给出正确的方向

希望这会对你有所帮助

您需要将寻呼机传递给两个视图控制器

pageOne.pager = self
pageTwo.pager = self