10

我的应用程序在 Core Data 对象中存储了不同的属性。它们都显示在 UITableView 中,如果单击单元格,则会显示 DetailView。Master-Detail-XCode-Template 中的常见内容。

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

到目前为止我得到了这个工作,但我不知道如何传递相应的数据

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location

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

到另一个 DetailViewController。

如果您只是单击单元格(没有 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 中,我都为其设置了相应的对象。

因此,我尝试获得与工作类似的东西,因此在我的 DetailViewController 中,我的“detailItem”需要来自选定(3D 触摸)单元格的相应核心数据对象。

感谢帮助 !

4

3 回答 3

8

如果您正在使用从单元到新视图控制器的故事板 segues,则发送者是表视图单元。

所以你可以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
    }
}
于 2015-11-06T22:28:51.050 回答
6

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

下面是我用来传递数据的代码。它在 Swift 中,它在目标 C 中应该是相似的。如果你想要目标 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
}
于 2015-10-08T23:42:17.133 回答
2

根据 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;

}

于 2015-10-10T14:28:54.440 回答