我有两个视图控制器。在mainViewController
中点击后,TabBarButton
用户转到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 Controller即可。当我运行程序并单击BarButton即时获取上述错误。
因为prepareForSegue无法识别它应该执行哪个按钮吗?怎么解决呢?
答案 0 :(得分: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
}
}
}