gpt4 book ai didi

ios - 在 Swift 中使用 UIPageViewController 水平滚动 ViewControllers 的按钮

转载 作者:行者123 更新时间:2023-11-29 11:24:55 30 4
gpt4 key购买 nike

我通过滑动手势将 PageViewController 更改为 ScrollViewController。它有效,但我想修改它并使用按钮滚动。

问题:在 Storyboard 中有三个 ViewControllers 但我不知道如何调用父类 PageViewController 并要求将当前 View 更改为下一个 View 。

例如,ViewController1 按钮“nextView” -> ViewController2。在直接调用 ViewController2 的情况下,PageViewController 控件可能会被破坏。是否正确?

ViewController1 的 UIButton 应该用什么代码?

PageViewController 代码:

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

lazy var orderedVC: [UIViewController] = {
return [self.newViewController(viewController: "ViewController1"),
self.newViewController(viewController: "ViewController2"),
self.newViewController(viewController: "ViewController3")]
}()

//Create the PageControl
var pageControl = UIPageControl()
func configurePageControl() {
pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
...
self.view.addSubview(pageControl)
}

override func viewDidLoad() {
super.viewDidLoad()

self.dataSource = self
if let firstViewController = orderedVC.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
self.delegate = self
configurePageControl()
}

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

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController:UIViewController) -> UIViewController? {

//Get index from the the views list BEFORE
guard let viewControllerIndex = orderedVC.firstIndex(of: viewController) else {return nil}
let previousIndex = viewControllerIndex - 1

//Limitations of the index
guard previousIndex >= 0 else {return nil}
guard orderedVC.count > previousIndex else {return nil}

return orderedVC[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController:UIViewController) -> UIViewController? {

//Get index from the the views list AFTER
guard let viewControllerIndex = orderedVC.firstIndex(of: viewController) else {return nil}
let nextIndex = viewControllerIndex + 1

//Limitations of the index
guard orderedVC.count != nextIndex else {return nil}
guard orderedVC.count > nextIndex else {return nil}

return orderedVC[nextIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousVC: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
self.pageControl.currentPage = orderedVC.firstIndex(of: pageContentViewController)!
}
}

最佳答案

可以使用自定义协议(protocol)并使您的 PageView Controller 成为 View Controller 的委托(delegate)。

protocol PageViewDelegate {
func previousPage()
func nextPage()
}

class ViewController: UIViewController {
var pageViewDelegate: PageViewDelegate?
@IBAction func previousButtonTapped(_ sender: Any) {
pageViewDelegate?.previousPage()
}
@IBAction func nextButtonTapped(_ sender: Any) {
pageViewDelegate?.nextPage()
}
}

class PageViewController: UIPageViewController, PageViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// After the viewControllers have been setup
self.viewControllers?.forEach {
if let controller = $0 as? ViewController {
controller.pageViewDelegate = self
}
}
}
func previousPage() {
//show previous page
}
func nextPage() {
//show next page
}
}

阅读更多关于协议(protocol)的信息 here .

关于ios - 在 Swift 中使用 UIPageViewController 水平滚动 ViewControllers 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426632/

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