我有一个 MainViewController
,其中有一个容器 View ,其中嵌入了一个 UIPageViewController
。 MainViewController
符合协议(protocol) UIPageViewControllerDelegate, UIPageViewControllerDataSource
我希望嵌入的 UIPageViewController
由 MainViewController
委托(delegate)。
这可能吗?
基本上,我试图以类似的态度将 View Controller 中的 Collection View 作为 Storyboard 的导出,然后为其设置委托(delegate)(collectionView.delegate = self
,假设 self 委托(delegate))
所以这个态度用了一个UIPageViewController
。
我遇到的障碍是没有 PageView
作为 PageViewController
的对立面(与 Collection View 相比,我有 CollectionView
而不是 Collection View Controller
。
最佳答案
以下是您可以用来生成此内容的步骤:
您可以通过覆盖 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/