gpt4 book ai didi

ios - 用按钮移动 pageViewController

转载 作者:行者123 更新时间:2023-11-28 06:08:11 26 4
gpt4 key购买 nike

基本上,我不仅希望通过滑动移动我的 PageViewController,还希望通过按钮移动我的 PageViewController。

@IBAction func nextButtonAction(_ sender: Any){

}

My PageViewController looks like in this guide

我有什么:

  • 页面 View Controller
  • 3 个 View Controller

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

tried to call UIPageViewControllerDataSource on each ViewController but didn't work also I think this is not the best approach

已编辑:这是我的 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)
}
}

最佳答案

要通过单击按钮将页面 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()
}

希望对你有帮助

关于ios - 用按钮移动 pageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com