2017-01-03 32 views
2

我想窥视一个视图控制器,它只有一个ImageView和一些标签,但当用户按下更难时,他们会弹出到ViewController,他们通常会已经有用户只需点击表格视图单元格!Peek ViewController但弹出一个不同的ViewController - 三维Touch - Swift

我遇到的问题是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 

} 

回答

2

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

+1

我明白了,viewControllerToCommit中的信息包含我已经存储在previewViewController中的属性?我尝试使用viewControllerToCommit.storyImage,那里没有任何东西,因为它在我看来是错误的?我是否了解你> – Eli

+1

你不能说'viewControllerToCommit.storyImage'。你必须对它真正的课程进行研究。如果这是一个PeekViewController,你会说'(viewControllerToCommit as!PeekViewController).storyImage'。 – matt

+0

作品魅力!谢谢 :) – Eli

相关问题