swift - 如何在嵌入 ViewController 的容器 View 中设置 UIPageViewController 的委托(delegate)

标签 swift uipageviewcontroller

我有一个 MainViewController,其中有一个容器 View ,其中嵌入了一个 UIPageViewControllerMainViewController 符合协议(protocol) UIPageViewControllerDelegate, UIPageViewControllerDataSource 我希望嵌入的 UIPageViewControllerMainViewController 委托(delegate)。 这可能吗?

基本上,我试图以类似的态度将 View Controller 中的 Collection View 作为 Storyboard 的导出,然后为其设置委托(delegate)(collectionView.delegate = self,假设 self 委托(delegate)) 所以这个态度用了一个UIPageViewController。 我遇到的障碍是没有 PageView 作为 PageViewController 的对立面(与 Collection View 相比,我有 CollectionView 而不是 Collection View Controller

enter image description here

最佳答案

以下是您可以用来生成此内容的步骤:

您可以通过覆盖 MainViewController 中的 func prepare(for segue: UIStoryboardSegue, sender: Any?) 来获取对 UIPageViewController 的引用。当您的嵌入式 Controller 成为子 Controller 时,将触发此功能。

如果你给了你的 segue 一个名字(让我们使用 pageSegue 作为这个例子),你可以在 MainViewController 中做这样的事情:

func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "pageSegue", let controller = segue.destination as? UIPageViewController {
        controller.delegate = self
        controller.dataSource = self
    }

}

关于swift - 如何在嵌入 ViewController 的容器 View 中设置 UIPageViewController 的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062702/

相关文章:

swift - 在 ScrollView 中禁用弹跳 在 pageView Controller 中禁用水平滚动

swift - 无法使用类型为 'URLSession' 的参数列表调用 '(configuration: URLSessionConfiguration)'

ios - 从给定范围和范围值 : Min= 0, Max= 999999 中找到剩余的 KM

memory-leaks - UIPageViewController内存泄漏

iOS UIPageControl 改变位置

ios - 为什么我在运行 PageViewController 后会收到此错误?

ios - UIPageViewController 作为 subview ,横向双页,纵向单页

ios - 删除单元格之前提醒 Controller Swift Xcode

ios - 如何在 swift 5 xcode 10 中创建在多个 View 中使用的单例 View

ios - 当从 iOS 13.1 中的 SceneDelegate 设置 RootViewController 时,UIElements 变为非事件状态