【问题标题】:move pageViewController with button用按钮移动 pageViewController
【发布时间】:2017-11-30 11:19:41
【问题描述】:

基本上,我想移动我的 PageViewController 不仅可以通过滑动来移动,还可以通过按钮来移动。

@IBAction func nextButtonAction(_ sender: Any){

}

My PageViewController looks like in this guide

我有什么:

  • PageViewController
  • 3 个视图控制器

抱歉,如果是重复的,没有找到完全相同的情况

试图在每个 ViewController 上调用 UIPageViewControllerDataSource 但也没有用我认为这不是最好的方法

已编辑: 这是我的 PageViewController

class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    lazy var VCarr: [UIViewController] = {

        return [self.VCInstance(name: "signUpVerification"),
        self.VCInstance(name: "signUpVerification1"),
        self.VCInstance(name: "signUpVerification2"),
        ]
    }()

    func VCInstance(name:String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:name)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self
        if let firsVC = VCarr.first {

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

        }
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCarr.index(of: viewController) else { return nil }
        let previousIndex = viewControllerIndex - 1
        guard previousIndex >= 0 else {
            return nil
        }
        return VCarr[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = VCarr.index(of: viewController) else { return nil }
        let nextIndex = viewControllerIndex + 1
        guard nextIndex < VCarr.count else {
            return nil
        }
        return VCarr[nextIndex]
    }



}

extension PageVC {
    func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        if let currentViewController = VCarr.first {
            if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) //for some reasons there's nil {
                setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
            }
        }
    }
}

这是我称之为 Viewcontroller 的地方:

var pageVC = PageVC()
@IBAction func nextButtonAction(_ sender: Any){

   pageVC.goToNextPage()

}

解决方案:

YourViewController: {
 @IBAction func nextButtonAction(_ sender: Any){
        let pageViewController = self.parent as! PageVC
        pageViewController.nextPageWithIndex(index: 1)


    }
}

PageViewController: {
   func nextPageWithIndex(index: Int) {
        setViewControllers([VCarr[index]], direction: .forward, animated: true, completion: nil)
    }
}

【问题讨论】:

标签: ios swift uipageviewcontroller


【解决方案1】:

要通过按钮单击将页面 viewController 移动到,您可以使用以下扩展非常简单地实现

extension UIPageViewController {
    func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        if let currentViewController = viewControllers?[0] {
            if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
                setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
            }
        }
    }

    func goToPreviousPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        if let currentViewController = viewControllers?[0] {
            if let previousPage = dataSource?.pageViewController(self, viewControllerBefore: currentViewController){
                setViewControllers([previousPage], direction: .reverse, animated: true, completion: completion)
            }
        }
    }
}

继续前进

self.pageViewController.goToNextPage()

向后移动

self.pageViewController.goToPreviousPage()

别忘了检查索引是否可用

例如:

if pageArray.count > currentIdex {
self.pageViewController.goToNextPage()
}

希望对你有帮助

【讨论】:

  • 感谢您的帮助,但不幸的是我收到了错误消息。 index 0 beyond bounds for empty NSArray
  • 您是在滑动下一页时发生崩溃还是仅在单击按钮时发生崩溃?
  • 如果你的意思是用手势滑动,那么它不会起作用,它应该是。按钮问题I get this error Unbalanced calls to begin/end appearance transitions
  • 仅带按钮
  • 我发现这很有帮助。谢谢!。全局保存当前索引,需要像 viewControllers 一样使用?[currentIndex]。相应地更新 UIPageViewController 委托上的 currentIndex。
【解决方案2】:

尝试在按钮 Action 中添加此方法,以便从第一个 View Controller 移动到第二个:

self.pageViewController.setViewControllers([self.viewControllerAtIndex(1)!], direction: .forward, animated: true, completion: nil)
pageControl2.currentPage = 1

您可以使用PageControl 添加当前的 PageTracker。

self.pageViewController.setViewControllers([self.viewControllerAtIndex(pageControl.currentPage + 1)!], direction: .forward, animated: true, completion: nil)
pageControl.currentPage += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多