Swift - 在一个UIViewController中实现PageViewController

时间:2014-11-30 14:27:36

标签: ios swift uiviewcontroller uipageviewcontroller

在互联网上没有解释如何在你的UIViewController中实现PageViewController 我从默认的XCode基于页面的应用程序中获取此代码

class ViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    var pageViewController: UIPageViewController?
    override func viewDidLoad() {
        self.pageViewController = UIPageViewController(transitionStyle: .PageCurl, navigationOrientation: .Horizontal, options: nil)
        self.pageViewController!.delegate = self
        self.view.addSubview(self.pageViewController!.view)
        // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
        var pageViewRect = self.view.bounds
        self.pageViewController!.view.frame = pageViewRect
        self.pageViewController!.didMoveToParentViewController(self)
        // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
        self.view.gestureRecognizers = self.pageViewController!.gestureRecognizers
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
     //?? what controller should I return
    }
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    //?? what controller should I return
    }
}

我想要6页,我想在不同页面添加文字,以便识别我在哪个页面并显示我想要的信息?我只想使用1个ViewControler,只使用这个视图控制器。

1 个答案:

答案 0 :(得分:2)

UIPageViewController要求您使用不同的视图控制器。您可能需要重构代码才能拥有父母"或" root"视图控制器,其唯一的工作是呈现实现特定页面逻辑的其他视图控制器。这是一些示例代码。它过于简单,但应该让你知道如何开始。

class PageContent {
    let text: String
    let index: Int
    init(text: String, index: Int) {
        self.text = text
        self.index = index
    }
}

class MyPageViewController : UIViewController {
    var page: PageContent!
    override func viewDidLoad() {
        // Load page data for self.page
    }
}

class ViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    var pageViewController: UIPageViewController?
    var pages: [PageContent]!

    override func viewDidLoad() {
        let firstPage = PageContent(text: "...", index: 0)
        let secondPage = PageContent(text: "...", index: 1)
        // ...
        self.pages = [firstPage, secondPage] // add more as necessary
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        let beforeController = viewController as MyPageViewController
        let newPageIndex = beforeController.page.index - 1
        if newPageIndex >= 0 {
            let newController = MyPageViewController()
            newController.page = self.pages[newPageIndex]
            return newController
        } else {
            return nil
        }
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        let afterController = viewController as MyPageViewController
        let newPageIndex = afterController.page.index + 1
        if newPageIndex < self.pages.count {
            let newController = MyPageViewController()
            newController.page = self.pages[newPageIndex]
            return newController
        } else {
            return nil
        }
    }
}