2016-04-20 84 views
0

我目前有一個按鈕設置爲轉到TableViewController,但決定我想將TableViewController嵌入到TabBarController中。在我做這件事之前,我在之前的ViewController中已經有了這些代碼。prepareForSegue目標TableViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if(segue.identifier == "showListSegue"){ 
     let des = segue.destinationViewController as! TableViewController 
     des.jsonfile = self.jsonfile 
    } 
} 

現在,當我嘗試點擊按鈕,它給了我這個錯誤。

無法投類型 '的UITabBarController'(0x111c818b0)至 '的值Forkit.TableViewController'(0x10d1b1490)。

我嘗試下面的代碼,但沒有成功,因爲它給了我這個錯誤

價值型「的UITabBarController」有沒有成員「jsonfile」。

let des = segue.destinationViewController as! TableViewController 

我怎樣才能解決這個問題?

+0

你需要做順着接下去指向'TableViewController'而不是'UITabBarController'? – Zhao

回答

-4

嘗試這個

@IBAction func about(sender: AnyObject) { 
     performSegueWithIdentifier("about", sender: sender) 
} 
2

的賽格瑞showListSegue現在有你的TabBarController,因爲它是destinationViewController。這就是爲什麼它不能投射到TableViewController,因爲它不是你要找的那個。 TabBarController確實包含了你想要的ViewController的引用。你可能有兩種選擇:

1:使用.viewControllers,找到你的觀點在裏面,像這樣:

let tabBarController = segue.destinationViewController as! UITabBarController 
let des = tabBarController.viewControllers![0] 

在哪[0]選擇您需要的選項卡的視圖控制器。

2:選擇正確的選項卡,使用.selectedViewController

let tabBarController = segue.destinationViewController as! UITabBarController 
tabBarController.selectedIndex = 0 // choose which tab is selected 
let des = tabBarController.selectedViewController! 

請注意,您在這裏可選值工作,所以無論是絕對確保你可以安全地拆開包裝,或建立檢查(鑄造與as?if let陳述等)。

的更多信息:UITabBarController Class Reference

+0

我將如何傳遞des.jsonfile = self.jsonfile到那麼?我收到這個錯誤。 'UIViewController'類型的值沒有成員jsonfile –

+0

我想用TableViewController來做到這一點 –

+0

在這種情況下,你必須輸入cast:'let des = tabBarController.viewControllers![0] as! TableViewController' – Oscar