将数据从 UIViewController 传递到 UIPopover
Passing data from UIViewController to a UIPopover
我有一个 UITableView
,其自定义单元格填充有 NSManagedObjects
。我在我的自定义单元格上配置了一个按钮,以便在选择时显示弹出窗口。
我最初尝试使用 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)
}
}
以下是我计划获取要传递给弹出窗口的对象的方法 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
来传递它。我发现这些答案很有帮助:
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
以在 popOver 上设置标签等。
您不能通过 NSUserDefaults
传递 NSManagedObjects
。 This answer 让我在这方面摆平了。
You can only store things like NSArray, NSDictionary, NSString,
NSData, NSNumber, and NSDate in NSUserDefaults.
为了我的目的,我可以从我的 NSManagedObject
中传递属性,因为它们只有一种方式。
我有一个 UITableView
,其自定义单元格填充有 NSManagedObjects
。我在我的自定义单元格上配置了一个按钮,以便在选择时显示弹出窗口。
我最初尝试使用 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)
}
}
以下是我计划获取要传递给弹出窗口的对象的方法 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
来传递它。我发现这些答案很有帮助:
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
以在 popOver 上设置标签等。
您不能通过 NSUserDefaults
传递 NSManagedObjects
。 This answer 让我在这方面摆平了。
You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults.
为了我的目的,我可以从我的 NSManagedObject
中传递属性,因为它们只有一种方式。