将数据从UIViewController传递到UIPopover

时间:2015-11-06 23:51:30

标签: ios swift uitableview uiviewcontroller uipopovercontroller

我有一个UITableView,其自定义单元格填充了NSManagedObjects。我在自定义单元格上配置了一个按钮,以便在选中时显示弹出窗口。

我最初尝试使用performSegueWithIdentifierprepareForSegue来实现弹出窗口,但是UIPopover似乎不喜欢来自UITableViews的动态内容。通过self.presentViewController实例化popover对我有用,但我很难确定如何将数据传递给它。

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

以下是我计划如何抓住我希望传递给popover的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

如何从ViewControllerPopoverViewController获取对象?我应该通过这个物体还是我应该去另一条路?

1 个答案:

答案 0 :(得分:0)

由于除了以一种方式传递对象之外我做得不多,所以我最终使用NSUserDefaults来传递它。我发现这些答案很有帮助:

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

在我viewDidLoad的{​​{1}}中,我访问popoverVC以在popOver上设置标签等。

您无法通过NSUserDefaults传递NSManagedObjectsThis answer让我在那方面摆平。

  

你只能存储像NSArray,NSDictionary,NSString这样的东西,   NSUserDefaults中的NSData,NSNumber和NSDate。

出于我的目的,我可以通过NSUserDefaults传递属性,因为它们只是单向传播。