2017-09-23 71 views
0

我試圖在導航控制器中嵌入的ViewController之上呈現嵌入NavigationController中的tableViewController。嘗試在導航控制器之上呈現導航控制器「其視圖不在窗口層次結構中」

I keep getting the error: that the view controller cannot be presented because it is not in the window hierarchy.

總之,我想允許用戶點擊the number of likes上後,然後被引導至NavigationController嵌入式TableViewController和它不工作。

由於某種原因,當它從標籤欄控制器呈現時工作,當我嘗試點擊類似的數字,但不是當我從導航控制器執行它時。對於點擊等等數如下,位於集合視圖中的細胞迅速的文件的操作代碼:

@IBAction func LikeNumber_tapped(_ sender: Any) { 
     let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LikedByVC2") as? UINavigationController 
     let tableVC = vc?.viewControllers.first as! LikedByTableViewController 
     tableVC.postKey = self.postKey 
     self.window?.rootViewController?.present(vc!, animated: true, completion: nil) 

    } 

是否有可能做什麼我試圖?我試圖讓問題儘可能簡單,但讓我知道是否需要詳細說明。提前致謝。

+0

爲什麼要那樣做?爲什麼不''self.navigationController?.pushViewController(vc,animated:false)'?或者爲什麼不直接使用segue? – Paulw11

+0

@ Paulw11我無法使用push,因爲「self」是一個collectionviewcell,所以我僅限於顯示另一個視圖控制器的選項。而且我不確定segue如何幫助我顯示視圖控制器。 – dombad

+0

好的,如果'self'是一個單元格,那麼你應該使用委託和按鈕點擊事件到正確的視圖控制器,以便它可以被處理。基本方法[這裏](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-taped/38941510# 38941510)。顯示視圖控制器是segues所做的。 – Paulw11

回答

0

一個解決辦法是,以取代本的最後一行:

UIApplication.shared.keyWindow?.rootViewController?.present(vc!, animated: true, completion: nil) 

欲瞭解更多信息和各種解決方案,你也可以檢查此thread

+0

我仍然得到相同的錯誤。它表示如下:「警告:試圖在上呈現,其視圖不在窗口層次結構中!」它必須與從tabbarcontroller到導航控制器的事實有關,然後我想要展示的應該是通過該導航控制器,但試圖在tabbarcontroller上展示自己。這是我遇到的問題。 – dombad

相關問題