2017-10-04 34 views
0

我試圖從一個tabBarController第二tabBarController傳遞數據,但我得到的錯誤,我試過很多方法,但數據不經過無法投類型的UINavigationController的「價值tabBarController

是他們無論如何傳遞標籤之間的數據?

我與此代碼嘗試,但我得到的錯誤「類型的UINavigationController的「無法施展值CategoriesViewController」

enter code here 



    let barViewControllers = self.tabBarController?.viewControllers 

    let svc = barViewControllers![1] as! CategoriesViewController 

    svc.categoriesData = self.categoryProductsDict //shared model 
+1

這樣想:標籤欄 - >導航器 - >控制器。所以你有了導航控制器,你需要循環來獲得你的控制器。 –

回答

1
let svc = (barViewControllers![1] as! UINavigationController).viewControllers[0] 

我相信所有的標籤都viewControllers嵌入式導航控制器。所以當你從標籤viewController訪問viewController時,你會得到UINavigationController。爲了獲得VC你在索引0 NavigationController

更清潔更安全的代碼來獲取viewControllers

if let vc = (self.tabBarController.viewControllers![0] as? UINavigationController)?.viewControllers[0] as? CategoriesViewController { 
    //access your VC here        
} 
相關問題