无法将数据从 UITableviewController 传递到 ViewController - 不断收到 Swift 动态 Cast Class 无条件错误

Can't pass data from UITableviewController into ViewController - keep getting Swift dynamic Cast Class Unconditional error

我只是想将显示在表 ViewController 中的核心数据传递给另一个 ViewController - 我的 prepareForSegueCode 似乎有问题。尝试传递数据并将其加载到 ViewController 的 viewDidLoad 中。非常感谢任何帮助 - 已经为此工作了几个小时但没有运气。

表格ViewController代码:

import UIKit
import CoreData

class MyEventsTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

var myEvents:[myEventsData] = []

var fetchResultController: NSFetchedResultsController!

func controllerWillChangeContent(controller: NSFetchedResultsController!) {
    tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!,atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!){

    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    case .Update:
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    default:
        tableView.reloadData()
    }

    myEvents = controller.fetchedObjects as [myEventsData]

}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.endUpdates()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "myEventsCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyEventsTableViewCell

    let eventData = myEvents [indexPath.row]
    cell.eventNameLabel.text = "Name: " + eventData.eventName
    cell.startDateLabel.text = "Start: " + eventData.startDate
    cell.endDateLabel.text = "End: " + eventData.endDate

    cell.eventData = eventData

    cell.backgroundColor = UIColor.clearColor()



    return cell
}

override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "showEventDetails" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {

        let destinationController = segue.destinationViewController as MyEventDetailsViewController

        destinationController.eventData! = myEvents[indexPath.row]
        }
           }
}

目的地ViewController代码:

import UIKit

class MyEventDetailsViewController: UIViewController, UITableViewDelegate {


var eventData:myEventsData!


@IBOutlet weak var eventName: UILabel!
@IBOutlet weak var startDate: UILabel!
@IBOutlet weak var endDate: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var postComment: UIButton!
@IBOutlet weak var checkIn: UIButton!
@IBOutlet weak var addPhoto: UIButton!
@IBOutlet weak var myWineJournal: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.eventName.text = eventData.eventName
    self.startDate.text = eventData.startDate
    self.endDate.text = eventData.endDate
    self.details.text = eventData.details

}

我看到这一行有错误:

destinationController.eventData! = myEvents[indexPath.row]

因为您正在使用强制解包运算符进行赋值 - 这不是必需的,因为它应该只是:

destinationController.eventData = myEvents[indexPath.row]

由于 eventData 属性 似乎没有被初始化,我希望有一个不同的异常:

fatal error: unexpectedly found nil while unwrapping an Optional value


附录

转换异常的原因可能是:

let destinationController = segue.destinationViewController as MyEventDetailsViewController

您确定已在 IB 中为该视图控制器设置自定义 class 吗?

更新

如评论中所述,问题出在源视图控制器和目标视图控制器之间有一个导航控制器。该问题已如 this answer 中所述得到解决,包括从导航控制器检索顶视图控制器。