2015-07-11 108 views
1

我有兩個視圖控制器。在mainViewControllerTabBarButton當它點擊用戶轉到1stViewController並且有UIButton時它點擊用戶轉到2ndViewController兩個視圖控制器之間的導航

在使我的2ndViewController之前一切正常。但在我添加segue函數後,我的程序中出現錯誤。

could not cast value of type 'Calculator.1stViewController' (0x791a8) to 'Calculator.2ndViewController' (0x794c8).

簡單我的代碼是

@IBAction func CalculateGpa(){ 
//Calucation Goes Here 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     var AnotherName : 2ndViewController = segue.destinationViewController as! 2ndViewController 
//Code here 
} 

UIButton的執行SEGUE和BarButton什麼也不做只是去1stView控制器。當我運行該程序,然後單擊BarButton即時獲取上述錯誤。

它是因爲prepareForSegue不能識別哪個按鈕它應該執行正確?如何解決它?

回答

2

試試這段代碼。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if (segue.identifier == "youridentifiername") { 
     //your class name 
     if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController { 
      viewController.dealEntry = deal 
     } 

    } 
} 
+0

我更新我的代碼試試這種方式來實現@axonlivelabs –

+0

它的工作。謝謝 – chathura

相關問題