2015-11-06 78 views
0

我有一个UITableView的定制单元格填充NSManagedObjects。我在自定义单元格上配置了一个按钮,以便在选择时显示弹出窗口。从UIViewController传递数据到UIPopover

我最初尝试使用performSegueWithIdentifierprepareForSegue来实现弹出窗口,但UIPopover segues似乎不喜欢来自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) 
    } 
} 

下面是我打算如何获得想要传递给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?我应该通过对象还是有另一条路线我应该去?

回答

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

在我popoverVCviewDidLoad,我访问NSUserDefaults设置标签等上酥料饼。

您无法通过NSManagedObjects通过NSUserDefaultsThis answer让我在那个面前摆平了。

只能存储之类的NSArray,NSDictionary的,的NSString,NSData的 ,的NSNumber,并在NSDate的NSUserDefaults的。

出于我的目的,我可以通过从我的NSManagedObject传递的属性逃脱,因为他们只有一种方式。

相关问题