我有一個UITableView
的定製單元格填充NSManagedObjects
。我在自定義單元格上配置了一個按鈕,以便在選擇時顯示彈出窗口。從UIViewController傳遞數據到UIPopover
我最初嘗試使用performSegueWithIdentifier
和prepareForSegue
來實現彈出窗口,但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
我將如何得到我的對象從ViewController
到PopoverViewController
?我應該通過對象還是有另一條路線我應該去?