Peek ViewController但是弹出不同的ViewController - 3D Touch - Swift

时间:2017-01-03 02:04:29

标签: swift uiviewcontroller 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

}

1 个答案:

答案 0 :(得分:4)

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