2017-01-03 29 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

相關問題