[英]how to send data from uiviewcontroller to uipopover when it is being launched?
[英]Passing data from UIViewController to a UIPopover
我有一个UITableView
其自定义单元格填充有NSManagedObjects
。 我已经在自定义单元格上配置了一个按钮,以使其在选择时显示弹出窗口。
我最初尝试使用performSegueWithIdentifier
和prepareForSegue
实现弹出窗口,但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.