2015-11-06 71 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傳遞的屬性逃脫,因爲他們只有一種方式。

相關問題