如何在Swift中控制UIPageViewController项索引?

如何在Swift中控制UIPageViewController项索引?,swift,uiviewcontroller,uipageviewcontroller,Swift,Uiviewcontroller,Uipageviewcontroller,我已经设置了一个UIPageViewController。因此,我有两个uiviewcontroller和一个UIPageViewController 我正在使用此代码,当用户向前或向后滑动时,我希望减少或增加一个名为“character”的变量 class PageItemController: UIViewController { /* As we’ll have only one class for all content controllers, we need to somehow

我已经设置了一个
UIPageViewController
。因此,我有两个
uiviewcontroller
和一个
UIPageViewController

我正在使用此代码,当用户向前或向后滑动时,我希望减少或增加一个名为“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
        }
    }
}
...
}
我正在做的是:

  • 点击检查按钮检查变量“character”的值
  • 正在显示“字符1”
  • 向右滑动
  • 点击检查按钮再次检查变量“character”的值
  • 正在显示“字符2”
  • 向左滑动
  • 点击检查按钮
  • 正在显示“字符2”。但是等等,该变量字符应该包含“字符1”
  • 那么,为什么会发生这种情况呢?它应该显示字符1而不是字符2

    如果您需要更多详细信息,请查看此视频:

    检查此答案:

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