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

前端 未结 3 500
天命终不由人
天命终不由人 2020-12-14 11:59

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-

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 12:28

    Get the destination View controller from the storyboard using its storyboard ID and pass the objects that you are doing in prepareForSegue method.

    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
    }
    

提交回复
热议问题