Tometoyou Tometoyou - 9 days ago 6
iOS Question

UIPageViewController delegate/datasource methods not firing

I've a set up as such:

UIWindow
->
UIViewController
->
UIPageViewController
->
UIViewController
.

I've set the delegate and datasource for the
UIPageViewController
to
self
inside the
UIViewController
, however every time I try to update the
UIPageViewController
with data, it will only display one image. I am scaling the parent
UIViewController
's frame up from small to large, perhaps that's affecting it? I have virtually identical code elsewhere thats not in a
UIWindow
and that works. Is there something about
UIWindow
s?

Any help would be appreciated!

--EDIT--

Ok a bit more info. I'm calling this from when a user selects a table view cell. First I create a new
UIWindow
that's the same size as the current main
UIWindow
but lies on top of the status bar. Then I create an
ExpandedViewController
from the storyboard, set it a frame size the same as the
UITableViewCell
, give it some images to load into the
UIPageViewController
, then tell it to reload its data. I also adjust the centre of a
UIImageView
inside the UIPageViewController's view controller which I can animate later, but that's irrelevant. Then I add the view of the
ExpandedViewController
to the
UIWindow
. Here's that in code:

let cell = tableView.dequeueReusableCellWithIdentifier("aCell", forIndexPath: indexPath) as! ContentTableCell

var cellRect = tableView.rectForRowAtIndexPath(indexPath)
var convertedFrame = tableView.convertRect(cellRect, toView: UIApplication.sharedApplication().keyWindow!)

newWindow = UIWindow(frame: self.view.window!.bounds)
newWindow.hidden = false
newWindow.windowLevel = (UIWindowLevelStatusBar + 1)

var expandedViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ExpandedViewController")! as! ExpandedViewController
expandedViewController.view.frame = convertedFrame
expandedViewController.pageImages = imagesForTableView
expandedViewController.reloadData(indexPath.row)

var expandedContentViewController = (expandedViewController.pageViewController.viewControllers.last! as! ExpandedContentViewController)

expandedContentViewController.imageView.frame = self.view.frame
expandedContentViewController.imageView.center = CGPointMake(cellRect.width/2, cellRect.height/2 + 5)

newWindow.backgroundColor = UIColor(red: 30/255, green: 1/255, blue: 42/255, alpha: 1)
newWindow.addSubview(expandedViewController.view)
newWindow.makeKeyAndVisible()


The
ExpandedViewController
has this code in its
ViewDidLoad()
method:

self.view.clipsToBounds = true

pageImages = []

// Initialise the Page View Controller from storyboard. Set ourselves as delegate and datasource to it.
pageViewController = storyboard!.instantiateViewControllerWithIdentifier("ExpandedPageViewController")! as! UIPageViewController
pageViewController.dataSource = self
pageViewController.delegate = self

// Get the View Controller for the first page of the Page View Controller and store in an array
if let startingViewController: ExpandedContentViewController = viewControllerAtIndex(0) {
let viewControllers: NSArray = [startingViewController]
// Set the first page of the Page View Controller
pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: false, completion: nil)
}

// Make sure the Page View Controller takes up full area of screen
pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)

// Add the Page View Controller to current view
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)

Answer

I solved it by replacing

newWindow.addSubview(expandedViewController.view)

with

newWindow.rootViewController = expandedViewController

It turns out .rootViewController is the recommended way for adding a view to a UIWindow