【问题标题】:Change Page When Tapping on Pagecontrol in Swift在 Swift 中点击 Pagecontrol 时更改页面
【发布时间】:2016-09-09 15:05:30
【问题描述】:

我知道这个问题之前已经在这里回答过,但我无法使用 Swift 找到答案。目前,我在容器视图中嵌入了一个 pageviewcontroller。容器视图与页面控件位于同一视图控制器内。我正在使用委托来使页面控件在来回分页时更改,但当前在点击页面控件点时页面不会更改。如何在 Swift 中做到这一点?

我在这里找到的答案回答了这个问题,但不是使用 pageviewcontroller。

【问题讨论】:

  • 这里有几个使用 Swft 的答案:stackoverflow.com/questions/13854222/…
  • 这些答案涉及使用集合视图。我正在使用 pageviewcontroller。
  • 从这些答案中引用:“单击 UIPageControl 的点会将当前视图向左或向右移动。”
  • 这里引用了之前那句话。 “这是 UICollectionView 的 Swift 2 解决方案”
  • 天哪,伙计。我带你去喝水……喝。该问题的另一个答案完全没有说明 UICollectionView,只有 UIPageControl。它有 50 个赞成票(所以这是一个相对较好的答案),这正是您在这里所要求的。 How do I change the page when clicking on the UIPageControl dots?

标签: ios swift uipageviewcontroller uipagecontrol


【解决方案1】:

我的情况与链接中提供的答案不同。我在容器视图中有一个 viewpagecontroller 并遵循 this 教程和 second one ,其中对页面控件进行了解释。

于是我带着容器视图去了父viewcontroller,并创建了一个全局变量:

var pagerController: MainPageViewController!

然后使用:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "PagerContainerSegue") {
        //This is used to reference the paging left and right functions
        let childViewController = segue.destination as! MainPageViewController
        pagerController = childViewController
    }
}

然后我在值改变时为页面控件添加了一个出口:

@IBAction func pageControlValueChange(_ sender: AnyObject) {
    //setViewControllers comes from the pageviewcontroller
    if(pageIndex == 0){
        pagerController.setViewControllers([orderedViewControllers[1]],
                                              direction: .forward,
                                              animated: true,
                                              completion: nil)
        pageIndex += 1

    }else{
        pagerController.setViewControllers([orderedViewControllers[0]],
                                               direction: .reverse,
                                               animated: true,
                                               completion: nil)
        pageIndex -= 1
    }
}

pageIndex 在这里赋值:

func mainPageViewController(_ mainPageViewController: MainPageViewController,
                                didUpdatePageIndex index: Int) {
    pageIndex = index
    pageControl.currentPage = index
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多