我只是试图将显示在TableViewController中的核心数据传递给另一个ViewController - 我的prepareForSegueCode似乎有问题。尝试传递数据并在ViewController的viewDidLoad中加载它。任何帮助都非常感激 - 一直在努力工作几个小时没有运气。
TableViewController代码:
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]
}
}
}
DestinationViewController代码:
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
}
答案 0 :(得分:1)
我在这一行看到错误:
destinationController.eventData! = myEvents[indexPath.row]
因为您正在使用强制解包运算符进行分配 - 这不是必需的,因为它应该只是:
destinationController.eventData = myEvents[indexPath.row]
由于eventData
属性似乎未初始化,我预计会遇到不同的异常:
致命错误:在解包可选值时意外发现nil
<强>附录强>
施放异常可能是由此引起的:
let destinationController = segue.destinationViewController as MyEventDetailsViewController
您确定已在IB中为该视图控制器设置了自定义类吗?
<强>更新强>
如评论中所述,事实证明问题在于源和目标视图控制器之间存在导航控制器。该问题已在this answer中解释,包括从导航控制器检索顶视图控制器。