将 UIPageViewController 的逻辑实现为 Swift 中 UIViewController 的子视图

Posted

技术标签:

【中文标题】将 UIPageViewController 的逻辑实现为 Swift 中 UIViewController 的子视图【英文标题】:Implementing UIPageViewController's logic as a subview to a UIViewController in Swift 【发布时间】:2016-01-04 22:27:11 【问题描述】:

我必须实现一个 UIPageViewController 作为我的 UIViewController 的子视图。 UIPageViewController 里面有一些逻辑和其他 ViewController。我的问题是如何连接下面这两条代码。

这是我的主要 UIViewController 代码:

class MyMainViewController: UIViewController 

    override func viewDidLoad() 
       super.viewDidLoad()
    
    override func didReceiveMemoryWarning() 
       super.didReceiveMemoryWarning()
       

还有我的 UIPageViewController 代码,它在他包含的 ViewControllers 之间实现了一个无限循环(我复制了整个代码,可能没有必要):

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
    


【问题讨论】:

【参考方案1】:

在您布局MyMainViewControllerTutorialPageViewController 的情节提要中,您需要将容器视图添加到MyMainViewController,然后使用Embed segue 嵌入TutorialPageViewController

MyMainViewController 显示时,它现在也会自动插入您的教程视图。

【讨论】:

以上是关于将 UIPageViewController 的逻辑实现为 Swift 中 UIViewController 的子视图的主要内容,如果未能解决你的问题,请参考以下文章

将页面控件设置到 UIPageViewController 的顶部

将删除滑动手势从 UIPageViewController 传递给 UITableView

如何将数据从 UIPageViewController 传递到它的“子视图”之一?

将 UIPageViewController 的逻辑实现为 Swift 中 UIViewController 的子视图

无法将自动布局约束添加到 UIPageViewController

如何将 UITableView 或 UITableViewController 集成到 UIPageViewController 中?