I have a UITableView
whose custom cells are populated with NSManagedObjects
. I've configured a button on my custom cells to display a popover when selected.
I initially tried implementing a popover using performSegueWithIdentifier
and prepareForSegue
, but UIPopover segues don't seem to like dynamic content from UITableViews. Instantiating the popover via self.presentViewController
is working for me, but I'm having a difficult time figuring out how to pass data to it.
func demoButtonAction(sender: AnyObject) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("demoPopover")
vc?.modalPresentationStyle = .Popover
vc?.preferredContentSize = CGSizeMake(tableView.bounds.width * 0.75, tableView.bounds.height * 0.75)
if let presentationController = vc?.popoverPresentationController {
presentationController.delegate = self
presentationController.permittedArrowDirections = .Any
// sender is a UIButton on a custom cell
presentationController.sourceView = sender as? UIView
presentationController.sourceRect = CGRectMake(tableView.bounds.origin.x, tableView.bounds.origin.y, sender.bounds.width, sender.bounds.height / 2)
self.presentViewController(vc!, animated: true, completion: nil)
}
}
Here's how I plan to get a hold of the object I'd like to pass to the popover's ViewController.
let button = sender as! UIButton
let view = button.superview
let cell = view?.superview as! MyCustomTableViewCell
let indexPath: NSIndexPath = self.tableView.indexPathForCell(cell)!
let myObjectIWantToPass = fetchedResultsController.objectAtIndexPath(indexPath) as! MyNSManagedObject
How would I get my object from the ViewController
to PopoverViewController
? Should I be passing the object or is there another route I should be going?