两个视图控制器之间的导航

时间:2015-07-11 04:54:38

标签: ios objective-c swift

我有两个视图控制器。在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无法识别它应该执行哪个按钮吗?怎么解决呢?

1 个答案:

答案 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
        }

    }
}