如何在uiviewcontroller / storyboard上使用uibutton在uipageviewcontroller中推进页面

时间:2023-01-17 21:41:36

I have a very specific use case that I am wondering if anyone has dealt with. I want to add a "Continue" button on my first time user experience UIPageViewController. This article (Swift iOS: how to trigger next page using buttons), touches if you want it to be on every page, but I am not planning that broad of use.

我有一个非常具体的用例,我想知道是否有人处理过。我想在我的第一次用户体验UIPageViewController上添加一个“继续”按钮。这篇文章(Swift iOS:如何使用按钮触发下一页),如果你希望它出现在每一页上都会触及,但我并没有计划那么广泛的使用。

I have a UIPageViewController, and 6 viewcontrollers that I currently can navigate through by swiping.

我有一个UIPageViewController和6个viewcontrollers,我目前可以通过滑动导航。

I've tried connecting the button to the view controller as a segue, but that presents it outside of the PageViewController. I've thought of trying to just push a right to left swipe onto the button click, but I couldn't quit figure that out.

我尝试将按钮作为segue连接到视图控制器,但是它在PageViewController之外呈现。我曾经想过试着按一下从右到左滑动按钮点击,但我无法退出。

Anyone have an idea on how to attack putting a single button on one of the view controllers and have it progress to the next view controller within the UIPageViewController?

任何人都知道如何攻击在其中一个视图控制器上放置一个按钮并让它进入UIPageViewController中的下一个视图控制器?


This was marked as duplicate, but there are no duplicate cases of this issue on SO. There are few similar, but nothing like it. If there is one, please link it. The currently linked version is an obj-c answer to a similar question with no explanation as to how to link the needed function to the view controller. That answer I can implement as a global button easily, but a single view controllers button is not covered by it.

这被标记为重复,但在SO上没有此问题的重复案例。几乎没有相似之处,但却没有。如果有,请链接。当前链接的版本是对类似问题的obj-c答案,没有解释如何将所需功能链接到视图控制器。我可以轻松地将该答案实现为全局按钮,但单个视图控制器按钮不在其中。

Thanks


Edit for final code solution.

编辑最终代码解决方案。

So this was a two part solution coded in swift 3.

所以这是一个用swift 3编写的两部分解决方案。

In the uipageviewcontroller data source extension, the func to flip the page was built as follows.

在uipageviewcontroller数据源扩展中,翻页的func构建如下。

func goNextPage() {
   let cur = self.viewControllers![0]
   let p = self.pageViewController(self, viewControllerAfter: cur)
   self.setViewControllers([p!], direction .forward, animated: true, completion: nil)
  }

The piece in the individual view controller is built as follows

单个视图控制器中的部件构建如下

@IBAction func nextPageButtonWasTapped(_ sender: Any) {
  var candidate: UIViewController = self
  while true {
    if let pageViewController = candidate as? MyUIPageViewController {
        pageViewController.goNextPage()
        break
    }
    guard let next = parent else { break }
    candidate = next
  }
}

1 个解决方案

#1


1  

Walk up the view controller hierarchy to find the page view controller. When you find it, ask it to turn the page:

向上走视图控制器层次结构以查找页面视图控制器。当你找到它时,请它翻页:

@IBAction func nextPageButtonWasTapped(_ sender: AnyObject?) {
    var candidate: UIViewController = self
    while true {
        if let pageViewController = candidate as? UIPageViewController {
            candidate.navigateForward()
            break
        }
        guard let next = parentViewController else { break }
        candidate = next
    }
}

Implement navigateForward in an extension on UIPageViewController, based on the Objective-C version from this answer.

在UIPageViewController的扩展中实现navigateForward,基于此答案的Objective-C版本。

#1


1  

Walk up the view controller hierarchy to find the page view controller. When you find it, ask it to turn the page:

向上走视图控制器层次结构以查找页面视图控制器。当你找到它时,请它翻页:

@IBAction func nextPageButtonWasTapped(_ sender: AnyObject?) {
    var candidate: UIViewController = self
    while true {
        if let pageViewController = candidate as? UIPageViewController {
            candidate.navigateForward()
            break
        }
        guard let next = parentViewController else { break }
        candidate = next
    }
}

Implement navigateForward in an extension on UIPageViewController, based on the Objective-C version from this answer.

在UIPageViewController的扩展中实现navigateForward,基于此答案的Objective-C版本。