问题描述
我必须实现UIPageViewController作为UIViewController的子视图. UIPageViewController内部有一些逻辑和其他ViewController.我的问题是如何在下面连接这两段代码.
I have to implement a UIPageViewController as a subview to my UIViewController. The UIPageViewController has some logic and other ViewControllers inside. My question is how to connect these two pieces of code below.
这是我的主要UIViewController代码:
So this is my main UIViewController code:
class MyMainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
和我的UIPageViewController代码,该代码在他所包含的ViewController之间实现了无限循环(我复制了整个代码,可能没有必要):
and my UIPageViewController code, which implements an endless loop between ViewControllers that he contains (I copied the whole code, probably it wasn't necessary ):
class TutorialPageViewController: UIPageViewController {
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("Green"),
self.newColoredViewController("Red"),
self.newColoredViewController("Blue")]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController")
}
}
extension TutorialPageViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
推荐答案
在布置MyMainViewController
和TutorialPageViewController
的情节提要中,您需要将容器视图添加到MyMainViewController
,然后使用Embed
搜索以嵌入TutorialPageViewController
.
In the storyboard where you layout MyMainViewController
and TutorialPageViewController
, you will need to add a container view to MyMainViewController
and then use an Embed
segue to embed TutorialPageViewController
.
当显示MyMainViewController
时,它现在还将自动插入您的教程视图.
When MyMainViewController
is shown, it will now automatically insert your tutorial view as well.
这篇关于在Swift中将UIPageViewController的逻辑实现为UIViewController的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!