偷看ViewController,但弹出另一个ViewController-3D Touch-Swift

Eli*_*Eli 3 uiviewcontroller swift 3dtouch

我想窥视一个只包含ImageView和一些标签的视图控制器,但是当用户用力按下时,它们会弹出到ViewController中,这通常是用户只需单击表视图单元格就可以了!

我的问题是viewControllerToCommit没有索引路径,我无法确定要传递给新View Controller的内容。这是我到目前为止的代码:

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {

    let popViewController = self.storyboard?.instantiateViewController(withIdentifier: "ReadArticleViewController") as! ReadArticleViewController

    popViewController.storyURL = //This is where i need to be able to get the index path so i can extract the url for the webview

    self.show(popViewController, sender: self)

}

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    guard let indexPath = tableView.indexPathForRow(at: location) else {return nil}

    let cell = tableView.cellForRow(at: indexPath) as! ArticleCell

    let previewViewController = self.storyboard?.instantiateViewController(withIdentifier: "PeekViewController") as! PeekViewController

    previewViewController.storyImage = cell.pictureView.image
    previewViewController.storyTitle = cell.titleLabel.text

    previewViewController.preferredContentSize = CGSize(width: view.frame.width, height: 300)
    previewingContext.sourceRect = cell.frame

    return previewViewController

}
Run Code Online (Sandbox Code Playgroud)

mat*_*att 5

在中viewControllerForLocation,您拥有索引路径和单元格。您需要根据需要将该信息保存到实例属性中,以便commit在调用该属性时拥有它。如果此实例属性是PeekViewController的一部分,则将特别有帮助,因为这是交给您的commit(在名称下viewControllerToCommit)!它已经有一个storyImage财产和一个storyTitle财产;好吧,给它更多的属性,无论commit到达时将需要什么。换句话说,将PeekViewController用作信使(或换一种方式看,作为信封)。您从中返回viewControllerForLocation的实例是您将在commit中收到的实例viewControllerToCommit