简体   繁体   中英

UIPageViewController not showing anything but black screen - but doesn't throw errors

I am developing an application for iOS using swift. I have already set up one UIPageViewController successfully in my project, so I know how it works. But now I'm trying to set up a second PageViewController, and it just doesn't work - it doesn't throw any errors either, it just goes directly to black screen and doesn't even show any segues

What I have tried so far:

  1. Resetting the iOS simulator
  2. Reindexing the xcode files
  3. Restarting xcode
  4. Restarting my machine

None of these things have helped.

My code:

(BTW: This code is almost the exactly same as in my working UIPageViewController, the only difference is in the variables' names)

import UIKit

class SelectDestinationViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var destinationViewControllers : [UIViewController] = []

override func viewDidLoad() {
    super.viewDidLoad()

    println("View did appear")

    // Do any additional setup after loading the view.

    self.delegate = self
    self.dataSource = self

    let firstView = storyboard?.instantiateViewControllerWithIdentifier("firstView") as FirstDestinationViewController
    let secondView = storyboard?.instantiateViewControllerWithIdentifier("secondView") as SecondDestinationViewController

    destinationViewControllers = [firstView, secondView]

    for var index = 0; index < destinationViewControllers.count; ++index {

        println("\(destinationViewControllers[index])")

    }

    let startingViewController = self.viewControllerAtIndex(0)
    let viewControllers: NSArray = [startingViewController]

    self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: {(done: Bool) in})
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    println("View will appear!")
}

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

func viewControllerAtIndex(index:NSInteger) -> UIViewController {
    return destinationViewControllers[index]
}

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

    var index = find(destinationViewControllers, viewController)!

    if (index == 0) || (index == NSNotFound) {

        return nil

    }

    index--

    if index == destinationViewControllers.count {

        return nil

    }

    return self.viewControllerAtIndex(index)
}

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

    var index = find(destinationViewControllers, viewController)!

    if index == NSNotFound {

        return nil

    }

    index++

    if index == destinationViewControllers.count {

        return nil

    }

    return self.viewControllerAtIndex(index)
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {

    return destinationViewControllers.count

}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {

    return 0

}

}

All the prints are showing up in the console - but nothing else is happening. Does anyone know what the problem might be? Thank you in advance :)

Believe it or not the UIPageViewController class is actually not supposed to be viewed by itself. As with the split view controller class (but unlike the tab controller class) you need to have a master view which wraps up the page view controller.

You also need to do some inelegant tricks to get the page view controllers to load without being linked in via the storyboard. When you see code where someone has got it working you can tell they had to know way too much about UIViewController internals to get something so simple working..

It would be a bit clearer if, as with the split view controller, it showed you a standard set-up when you put it into your storyboard, but unfortunately the tab view, split view, and page view all behave very differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM