如何控制 Swift 中的 UIPageViewController itemIndex?

How do I control the UIPageViewController itemIndex in Swift?

我设置了一个UIPageViewController。因此我有两个 UIViewControllers 和一个 UIPageViewController.

我正在使用这段代码,我想在用户滑动向前[=40=时减少或增加一个名为"character"的变量] 或返回。

class PageItemController: UIViewController {

/* As we’ll have only one class for all content controllers, we need to somehow identify different content items. In order to do it, each  PageItemController should have its index and image name.*/

var itemIndex: Int = 0 {

    didSet {
        if itemIndex == 0 {
            character = 1
        }
        if itemIndex == 1 {
            character = 2
        }
    }
}
...
}

我正在做的是:

  1. 点击检查按钮检查变量的值"character"
  2. "Character 1" 正在显示
  3. 向右滑动
  4. 点击检查按钮再次检查变量的值"character"
  5. "Character 2"正在显示
  6. 向左滑动
  7. 点击检查按钮
  8. "Character 2" 正在显示。但是等等,那个可变字符应该包含 "Character 1" 而不是!

为什么会这样?它应该显示字符 1 而不是字符 2。

如果您需要更多详细信息,请观看此视频:https://www.dropbox.com/s/gd6c7p0xvclixcr/IMG_0886.mov?dl=0

检查这个答案:

您需要通过设置多个变量并在激活某些 UIPageControllers 方法时编辑它们的值来解决问题。