Ios SetViewController不';行不通

Ios SetViewController不';行不通,ios,swift,xcode,uipageviewcontroller,uicontainerview,Ios,Swift,Xcode,Uipageviewcontroller,Uicontainerview,我正在尝试使用“下一步”按钮更改UIPageViewController的当前ViewController。因此,我使用委托调用mainViewController中ContainerViewController中的函数。但它不执行setViewController行 在这里您可以看到我的代码: 这是我使用委托调用的方法: func forwardPage() { print("Start") currentPage += 1 print(vcs[currentPage]

我正在尝试使用“下一步”按钮更改UIPageViewController的当前ViewController。因此,我使用委托调用mainViewController中ContainerViewController中的函数。但它不执行setViewController行

在这里您可以看到我的代码:

这是我使用委托调用的方法:

func forwardPage() {
    print("Start")
    currentPage += 1
    print(vcs[currentPage])
    self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
        print("Done")
    }
}
protocol WalkthroughViewControllerDelegate {
   func forwardPage()
   func currentIndex() -> Int
}
这是我的代表:

func forwardPage() {
    print("Start")
    currentPage += 1
    print(vcs[currentPage])
    self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
        print("Done")
    }
}
protocol WalkthroughViewControllerDelegate {
   func forwardPage()
   func currentIndex() -> Int
}
下面是连接到“我的下一步”按钮的功能:

@IBAction func nextButtonTapped(_ sender: Any) {
    if let index = delegate?.currentIndex() {
        print("Here... \(index)")
        switch index {
        case 0...1:
            print("Forwarding...")
            delegate?.forwardPage()
        case 2:
            dismiss(animated: true, completion: nil)
        default: break
        }
    }

    updateUI()
}
除了“完成”之外的所有内容都会打印出来

我非常感谢你的帮助

我已经为此奋斗了很长一段时间了

非常感谢:)

编辑:这可能是因为UIPageViewController位于containerView中。但我不确定


第二次编辑:我为这个问题创建了一个git hub存储库。以下是链接:。我希望您能理解,我不会向您展示我的所有文件。

好的-问题是您在
viewdiload()中的代码:

正在创建一个新的
WalkthroughPageViewController
实例,一旦退出
viewDidLoad()
,该实例就会超出范围。因此它不再存在

您需要做的是在
prepare(for segue:…)
中获取对它的引用,并将它设置为您的代理:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? WalkthroughPageViewController {
        self.delegate = vc
    }
}

我将您的GitHub repo分叉,并将文件添加到项目中,这样您就可以看到它运行:

“我正在调用ContainerViewController中的函数”。。。
UIPageViewController
类中是否有
forwardPage()
?换句话说,
self
是否存在
UIPageViewController
的实例?以及。。。我用一个内嵌在containerView中的页面视图控制器进行了测试,所以这不是问题所在(除非func在错误的位置)。是的,
self
属于
UIPageViewController
Hmm类型。。。不知道为什么你的代码不起作用。你检查过你的车了吗?我有一个简单的示例项目,它使用一个嵌入式页面视图控制器,以及以编程方式更改页面的按钮。。。最后的完成块确实会被调用。你可以看一看,并将我的方法与你的方法进行比较,看看是否有什么结果:非常感谢你的帮助。不幸的是,我真的找不到任何能帮助我的东西。但无论如何,我真的非常感激:)非常感谢!我现在非常高兴:)真是太棒了,谢谢^^