UIPageViewController实现 - 更改视图控制器后的黑屏

时间:2015-05-14 05:07:15

标签: ios swift uiviewcontroller uipageviewcontroller

尝试成功实现一个允许用户使用UIPageViewController在UIViewControllers之间滚动的应用程序。

目前,在刷到第二个UIViewController后,屏幕变黑,我无法再滚动。有什么想法吗?

这是我的子类UIPageViewController代码:

class ApplicationViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var registrationController : UIViewController!
var loginController : UIViewController!
var captureController : UIViewController!
var visibleVCS = [UIViewController]()
var index = 0

override func viewDidLoad() {
    super.viewDidLoad()
    initControllers()
    reset()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func initControllers()
{
    registrationController = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewController") as UIViewController
    loginController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
    captureController = self.storyboard?.instantiateViewControllerWithIdentifier("CaptureViewController") as UIViewController

    visibleVCS = [registrationController, loginController, captureController]
}

func reset() {
    var currentController = viewControllerAtIndex(index)
    pageViewController.dataSource = self
self.setViewControllers([currentController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    if(index >= visibleVCS.count - 1){
        return nil
    }
    return self.viewControllerAtIndex(++index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if(index <= 0){
        return nil
    }
    return self.viewControllerAtIndex(--index)

}

func viewControllerAtIndex(index : Int) -> UIViewController? {
    return visibleVCS[index];
}
}

2 个答案:

答案 0 :(得分:3)

在UIPageViewController中将索引保存为属性也不适用于我。要解决这个问题,我必须创建一个带索引属性的基本UIViewController,如下所示:

class PageItemController: UIViewController {

var index = -1

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

然后将所有页面设为PageItemController的子类,并设置与其在数组中的位置对应的索引

func initControllers(){
    registrationController = self.storyboard?.instantiateViewControllerWithIdentifier("RegistrationViewController") as PageItemController
    registrationController.index = 0
    loginController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as PageItemController
    loginController.index = 1
    captureController = self.storyboard?.instantiateViewControllerWithIdentifier("CaptureViewController") as PageItemController
    captureController.index = 2
    // visibleVCS type must be [PageItemCotroller]
    visibleVCS = [registrationController, loginController, captureController]
}

最后在基于currentController索引的方法之前和之后实现

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemController

    if itemController.index > 0 {

        return visibleVCS[itemController.index - 1]

    }

    return nil

}


func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    let itemController = viewController as! PageItemController

    if itemController.index < visibleVCS.count - 1 {


        return visibleVCS[itemController.index + 1]
    }

    return nil
}

答案 1 :(得分:2)

@ Zellb的回答确实解决了这个问题。然而,这是我最终使用的代码,我发现它更易于管理:

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var index = indexOfViewController(viewController)!
    if (index != -1)
    {
        index++
        if (index >= visibleVCS.count)
        {
            return nil
        }
        else
        {
            return visibleVCS[index]
        }
    }
    return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var index = indexOfViewController(viewController)!
    if (index != -1)
    {
        index--
        if (index < 0)
        {
            return nil
        }
        else
        {
            return visibleVCS[index]
        }
    }
    return nil

}

func indexOfViewController(viewController : UIViewController) -> Int?
{
    for (var i = 0; i <= visibleVCS.count; i++)
    {
        if (viewController == visibleVCS[i])
        {
            return i
        }
    }
    return -1
}

其中visibleVC&#39; s只是我原始代码中的UIViewControllers数组。