didSelectRowAtIndexPath和IBAction的performSegueWithIdentifier

时间:2016-03-20 13:31:33

标签: xcode swift

代码将打开第一个ViewController,它工作正常:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("openDates", sender: self.dates[indexPath.row])
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let tasksViewController = segue.destinationViewController as! TasksViewController
    tasksViewController.selectedList = sender as! Dates
}

此代码打开第二个ViewController,但它不起作用:

    @IBAction func ShowDetails(sender: UIBarButtonItem) {
    self.performSegueWithIdentifier("openDetails", sender: sender)
}

错误讯息:

  

无法转换类型&dan; .DetailsViewController' (0x10b60b2e0)到' dante.TasksViewController' (0x10b60adc0)。

1 个答案:

答案 0 :(得分:3)

您正在调用两个单独的segue并在prepareForSegue中使用相同的代码,这意味着您正在尝试将两个单独的视图控制器转换为一个视图控制器。将您的代码更改为:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "openDates" {
        let tasksViewController = segue.destinationViewController as! TasksViewController
        tasksViewController.selectedList = sender as! Dates
    }
    else if segue.identifier == "openDetails" {
        let detailsViewController = segue.destinationViewController as! DetailsViewController 
        // Do stuff
        // If you're not doing stuff, remove this else if block
        // The if statement will still catch and prepare the other segue
    }
}