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

时间:2017-08-02 06:19:07

标签: ios swift uipagecontrol

我找了很多不同的解决方案,但没有一个能够通过按钮成功转换页面。其中,我找到的方法之一可能是成功的,但系统有一个未知的错误“不平衡调用开始/结束外观转换”,所以我想知道是否有可行的方法来通过按钮来转换网页?

斯威夫特3:

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

非常感谢:)

3 个答案:

答案 0 :(得分:0)

UIButton

的ViewController中创建这样的协议
import UIKit

protocol welcomeVCdelegate {
    func continueBtnPressed()
}

class welcomeVC: UIViewController {

    var delegate : welcomeVCdelegate?

然后在您的操作中调用您在protocol中创建的功能:

@IBAction func continueBtnAction(_ sender: UIButton) {
      self.delegate?.continueBtnPressed()
}

现在在主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
}

答案 1 :(得分:0)

使用下一个代码:

//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;
}

答案 2 :(得分:-1)

我的解决方案,根据需要在循环中循环页面:

       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)

    }