3D Touch Peek and Pop from UITableViewCell 如何将数据交给其他UIViewController?

3D Touch Peek and Pop from UITableViewCell how to hand over data to other UIViewController?

我的应用程序在核心数据对象中存储了不同的属性。它们都显示在一个 UITableView 中,如果您单击一个单元格,则会显示一个 DetailView。 Master-Detail-XCode-Template.

中的常见内容

现在我想用 peek 和 pop 实现 3D Touch。就像在邮件应用程序中一样,3D 触摸一个单元格,获得预览,更深地按下,弹出细节。

到目前为止我可以正常工作,但我不知道如何在

中传递相应的数据
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit

其他详情ViewController。

如果你只是点击单元格(没有3D触摸)我把数据交给

- (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 中,我为其设置相应的对象。

所以我尝试获得类似于工作的东西,所以在我的详细信息ViewController中,我的"detailItem"需要来自所选(3D 触摸)单元格的相应核心数据对象。

感谢帮助!

使用情节提要 ID 从情节提要中获取目标视图控制器,并在 prepareForSegue 方法中传递您正在执行的对象。

下面是我用来传递数据的代码。它在 Swift 中,在 objective C 中应该类似。如果您想要 Objective C 版本,请告诉我。

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
}

根据Dheeraj的回复,调整部分代码修正tableview单元格触摸位置错误。假设我们需要将名为 type 的值传递给目标控制器。

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
// check if we're not already displaying a preview controller
if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) {
    return nil;
}

CGPoint cellPostion = [self.tableview convertPoint:location fromView:self.view];
NSIndexPath *path = [self.tableview indexPathForRowAtPoint:cellPostion];

if (path) {
    UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:path];
    type = [[self.data objectAtIndex:path.row] integerValue];
    // shallow press: return the preview controller here (peek)
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DetailViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    previewController.type = type;
    previewingContext.sourceRect = [self.view convertRect:cell.frame fromView:self.tableview];
    return previewController;
}
return nil;

}

如果您使用从单元格到新视图控制器的故事板转接,则发送方是 table 视图单元格。

因此您可以这样实现 prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([sender isKindOfClass:[UITableViewCell class]]) {

        UITableViewCell *cell = sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        id viewController = segue.destinationViewController;

        // Get your data object
        // Pass it to the new view controller
    }
}