setViewControllers 不起作用

Len*_* P. 5 xcode ios uipageviewcontroller uicontainerview swift

我正在尝试使用 NEXT 按钮更改 UIPageViewController 的当前 ViewController。因此,我使用委托调用 mainViewController 中的 containerViewController 中的函数。但它不执行 setViewControllers 行。

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

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

func forwardPage() {
    print("Start")
    currentPage += 1
    print(vcs[currentPage])
    self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
        print("Done")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代表:

protocol WalkthroughViewControllerDelegate {
   func forwardPage()
   func currentIndex() -> Int
}
Run Code Online (Sandbox Code Playgroud)

这是连接到我的 NEXT 按钮的功能:

@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()
}
Run Code Online (Sandbox Code Playgroud)

除了“完成”之外的所有内容都会被打印

我将衷心感谢您的帮助

我已经为此苦苦挣扎有一段时间了

非常感谢 :)

编辑:也许发生这种情况是因为 UIPageViewController 位于 containerView 内。但我不确定

第二次编辑:我专门针对这个问题创建了一个 git-hub 存储库。这是链接: https: //github.com/LennartPhil/App-Popup-Screen。我希望您能理解我不会向您展示我的所有文件。

Don*_*Mag 2

好的 - 问题是你的代码viewDidLoad()

    let storyboard = UIStoryboard(name: "ExtraViewControllers", bundle: nil)
    let walkthroughPageVC = storyboard.instantiateViewController(withIdentifier: "WalkthroughPageVC") as! WalkthroughPageViewController
    delegate = walkthroughPageVC
Run Code Online (Sandbox Code Playgroud)

正在创建 的一个新实例WalkthroughPageViewController,该实例一旦viewDidLoad()退出就会超出范围。所以它已经不存在了。

您需要做的是在 中获取对它的引用prepare(for segue:...),并将其设置为您的委托:

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

我分叉了您的 GitHub 存储库并将文件添加到项目中,以便您可以看到它运行: https: //github.com/DonMag/App-Popup-Screen