是否可以使用 Button 更改 UIPageViewController 中的当前页面?

Is it possible to change current page in the UIPageViewController by using Button?

我找了很多不同的解决方案,但其中 none 可以通过按钮成功转换页面。其中,我找到的一种方法可能会成功,但系统出现未知错误"Unbalanced calls to begin / end appearance transitions",所以我想知道是否有可行的方法可以通过按钮转换页面?

Swift 3:

public func changeVC(VC: UIViewController) {
        self.setViewControllers([VC], direction: .forward, animated: true, completion: nil)
}

非常感谢:)

在 ViewController 中创建这样的协议,其中 UIButton

import UIKit

protocol welcomeVCdelegate {
    func continueBtnPressed()
}

class welcomeVC: UIViewController {

    var delegate : welcomeVCdelegate?

然后在您的操作中调用您在 protocol:

中创建的函数
@IBAction func continueBtnAction(_ sender: UIButton) {
      self.delegate?.continueBtnPressed()
}

现在在你的 main Class:

中给出协议
class TutorialVC: UIViewController, welcomeVCdelegate {

现在创建那个 class 的实例:

var pushVC: welcomeVC!

并给出 delegate 你所在的位置 instantiating :

 pushVC = self.storyboard?.instantiateViewController(withIdentifier: "welcomeVC") as! welcomeVC
 pushVC.delegate = self

现在在这里定义你的函数:

func continueBtnPressed() {
    print("continueBtnPressed")
      //Push to any view controller from here
}

使用下一个代码:

//array with all pages
private(set) lazy var orderedViewControllers: [UIViewController] = {
    return [
                Utils.newViewController(name: "PhotoOfTheDayViewController"),
                Utils.newViewController(name: "PhotosViewController"),
                Utils.newViewController(name: "PhotosViewController"),
                Utils.newViewController(name: "PhotosViewController"),
                Utils.newViewController(name: "InfoViewController")
    ]
    }();
private var currentIndex : Int!;

//jump to first page
@objc private func home() {
    self.jumptoPage(ind: 0);
}

private func jumptoPage(ind : Int) {

    var index = ind;
    let vc = self.orderedViewControllers.first;
    let direction : UIPageViewControllerNavigationDirection!;

    if currentIndex < index {
        direction = .forward;
        index += 1;
    } else {
        direction = .reverse;
        index -= 1;
    }

    if (currentIndex < index) {
        for i in 0 ..< index + 1 {
            if (i == index) {
                self.setViewControllers([vc!], direction: direction, animated: true, completion: nil);
            } else {
                self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil);
            }
        }
    } else {

        for i in stride(from: currentIndex, to: index - 1, by: -1) {
            if i == index {
                self.setViewControllers([vc!], direction: direction, animated: true, completion: nil);
            } else {
                self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil);
            }
        }


    }
    currentIndex = ind;
}

我的解决方案,如果需要的话循环页面:

       var pageIndex:Int?
    var pageControl:UIPageControl = UIPageControl()
    var pages = [UIViewController]()
    var pager:UIPageControl?

    var storyboardref: UIStoryboard {
        get{
            return UIStoryboard(name: "Tutorial", bundle: nil)
        }
    }

    var page1:UIViewController {
        get{
            return storyboardref.instantiateViewController(withIdentifier: "page01")
        }
    }
...
   override func viewDidLoad() {
        super.viewDidLoad()
...    
        pages.append(page1)
        pages.append(page2)

        setViewControllers([pages[0]], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
               pageControl.numberOfPages = pages.count
        pageControl.currentPage = pageIndex!
    }

   override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)?) {
        super.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion)
        pageIndex = pages.index(of: viewControllers![0])
        pageControl.currentPage = pageIndex!
    }    

    func nextPage() {
        let nextIndex = abs((pageIndex! + 1) % pages.count)
        self.setViewControllers([tutorial.pages[nextIndex]], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)    
    }

    func previousPage() {
        let prevIndex = abs((pageIndex! + pages.count - 1) % pages.count)
        self.setViewControllers([pages[prevIndex]], direction: UIPageViewControllerNavigationDirection.reverse, animated: true, completion: nil)

    }