从UITableViewCell 3D Touch Peek和Pop如何将数据移交给其他UIViewController? [英] 3D Touch Peek and Pop from UITableViewCell how to hand over data to other UIViewController?
问题描述
我的应用在Core Data对象中存储了不同的属性。它们都显示在UITableView中,如果单击一个单元格,则会显示DetailView。通常的东西,如Master-Detail-XCode-Template。
my app stores different attributes in Core Data objects. They are all shown in a UITableView, if you click a cell, a DetailView is shown. the usual stuff like in the Master-Detail-XCode-Template.
现在我想用偷看和弹出来实现3D Touch。就像在Mail应用程序中一样,3D触摸一个单元格,进行预览,按下更深处,弹出细节。
Now I want to implement 3D Touch with peek and pop. Like in the Mail app, 3D touch a cell, get preview, press deeper, pop in the detail.
我到目前为止工作得很好,但我无法弄清楚如何传递相应的数据
I got this working so far, but I can t figure out how to pass the corresponding data in
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
和
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
到另一个DetailViewController。
to the other DetailViewController.
如果你只是点击单元格(没有3D触摸)我交出数据
If you just click the cell (no 3D touch) I hand over the data in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
我使用的是
id detailItem
每个ViewController中的
我将相应的对象设置为它。
in every ViewController and I set the corresponding object to it.
所以我尝试得到类似的东西,所以在我的DetailViewController中,我的detailItem需要来自所选(3D触摸)单元格的相应Core Data对象。
So I try to get something similar to work, so in my DetailViewController my "detailItem" needs the corresponding Core Data object from the selected (3D touched) cell.
感谢您的帮助!
推荐答案
从故事板中获取目标View控制器使用其故事板ID并传递您在prepareForSegue方法中执行的对象。
Get the destination View controller from the storyboard using its storyboard ID and pass the objects that you are doing in prepareForSegue method.
下面是我用来传递数据的代码。它在Swift中,它应该在目标C中类似。如果你想要Objective C版本,请告诉我。
Below is the code that I use to pass the data. Its in Swift, it should be similar in objective C. Let me know if you want Objective C version.
func previewingContext(previewingContext: UIViewControllerPreviewing,viewControllerForLocation location: CGPoint) -> UIViewController? method:
// Obtain the index path and the cell that was pressed.
guard let indexPath = tableView.indexPathForRowAtPoint(location),
cell = tableView.cellForRowAtIndexPath(indexPath) else { return nil }
// Create a destination view controller and set its properties.
guard let destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("DestinationViewController") as? DestinationViewController else { return nil }
let object = fetchedResultController.objectAtIndexPath(indexPath)
destinationViewController.detailItem = object
/*
Set the height of the preview by setting the preferred content size of the destination view controller. Height: 0.0 to get default height
*/
destinationViewController.preferredContentSize = CGSize(width: 0.0, height: 0.0)
previewingContext.sourceRect = cell.frame
return destinationViewController
}
这篇关于从UITableViewCell 3D Touch Peek和Pop如何将数据移交给其他UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!