0

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?

Adrian
  • 16,233
  • 18
  • 112
  • 180

1 Answers1

0

Since I'm not doing much besides passing the object one way, I ended up using NSUserDefaults to pass it. I found these answers helpful:

Passing data between segues without using PrepareForSegue

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)

    // Get a hold of the managedObject I want to access from the popoverVC
    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

                NSUserDefaults.standardUserDefaults().setObject(myObjectIWantToPass.attribute, forKey: "objectToDemoAttribute")

    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)
    }
}

In viewDidLoad of my popoverVC, I access NSUserDefaults to set labels, etc. on the popOver.

You cannot pass NSManagedObjects via NSUserDefaults. This answer got me squared away on that front.

You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults.

For my purposes, I can get away with passing attributes from my NSManagedObject because they're only going one way.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180