繁体   English   中英

将数据从UIViewController传递到UIPopover

[英]Passing data from UIViewController to a UIPopover

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

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

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

这是我打算保留要传递给弹出框的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

我如何将我的对象从ViewController转到PopoverViewController 我应该通过物体还是应该走另一条路线?

由于除了以一种方式传递对象外,我没有做太多事情,因此我最终使用NSUserDefaults传递了它。 我发现这些答案很有帮助:

在segue之间传递数据,而无需使用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)
    }
}

在我的popoverVC viewDidLoad中,我访问NSUserDefaults以在NSUserDefaults上设置标签等。

您不能通过NSUserDefaults传递NSManagedObjects 这个答案使我直截了当。

您只能在NSUserDefaults中存储诸如NSArray,NSDictionary,NSString,NSData,NSNumber和NSDate之类的内容。

就我的目的而言,我NSManagedObject通过NSManagedObject传递属性,因为它们只是一种方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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