0

I'm simply trying to pass core data that displays in a TableViewController to another ViewController - there seems to be something wrong with my prepareForSegueCode. Trying to pass the data and load it in viewDidLoad of the ViewController. Any help is so much appreciated - been working on this for hours with no luck.

TableViewController Code:

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 Code:

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

}
David Berry
  • 40,941
  • 12
  • 84
  • 95
Mike Gibbs
  • 35
  • 1
  • 9
  • Is there a question? What results are you expecting? What results are you not seeing? Are you seeing a crash? – David Berry Feb 12 '15 at 20:13
  • @David: that is mentioned in the title. Mike, however I suggest to explicitly and clearly report the error in the question itself – Antonio Feb 12 '15 at 20:16
  • Check this out: https://github.com/iMac239/CoreDataSegueDemo – Ian Feb 12 '15 at 21:20

1 Answers1

1

I see an error in this line:

destinationController.eventData! = myEvents[indexPath.row]

as you are using the forced unwrapping operator for assignment - that's not necessary as it should simply be:

destinationController.eventData = myEvents[indexPath.row]

Since the eventData property doesn't seem to be initialized, I'd expect a different exception though:

fatal error: unexpectedly found nil while unwrapping an Optional value


Addendum

The cast exception could be caused by this:

let destinationController = segue.destinationViewController as MyEventDetailsViewController

Are you sure you have set the custom class in IB for that view controller?

Update

As outlined in comments, it turns out the problem is that there's a navigation controller between the source and the destination view controller. The problem has been fixed as explained in this answer, consisting of retrieving the top view controller from the nav controller.

Community
  • 1
  • 1
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Thanks Antonio - took out the exclamation point (a result of my many attempts to fix) and double checked and am sure the custom class is set in the view controller. – Mike Gibbs Feb 12 '15 at 21:11
  • libswiftCore.dylib`swift_dynamicCastClassUnconditional: 0x10b7f0860: pushq %rbp 0x10b7f0861: movq %rsp, %rbp 0x10b7f0864: testq %rdi, %rdi 0x10b7f0867: je 0x10b7f089e ; swift_dynamicCastClassUnconditional + 62 0x10b7f0869: movabsq $-0x7fffffffffffffff, %rax 0x10b7f0873: testq %rax, %rdi 0x10b7f0876: jne 0x10b7f089e ; swift_dynamicCastClassUnconditional + 62 – Mike Gibbs Feb 12 '15 at 21:13
  • FingerLakesUncorked`@objc FingerLakesUncorked.MyEventsTableViewController.prepareForSegue (FingerLakesUncorked.MyEventsTableViewController)(ObjectiveC.UIStoryboardSegue, sender : Swift.ImplicitlyUnwrappedOptional) -> () at MyEventsTableViewController.swift: – Mike Gibbs Feb 12 '15 at 21:13
  • Set a breakpoint on that line and inspect the actual type of the destination view controller – Antonio Feb 12 '15 at 21:15
  • Im not sure what you mean by "inspect the actual type" by doing this I also can see that there are no values in eventData as you already thought - ugh this is frustrating as I am not getting what I am doing wrong – Mike Gibbs Feb 12 '15 at 21:24
  • After the breakpoint is hit, from the variables view expand the `segue` variable, then `UIStoryboardSegue`, and check what's the type of the `_destinationViewController` property – Antonio Feb 12 '15 at 21:33
  • _destinationViewController UINavigationController * 0x7fe9d15a1bf0 0x00007fe9d15a1bf0 [0] UINavigationController – Mike Gibbs Feb 12 '15 at 21:46
  • That's the reason why if fails: the destination view controller is a `UINavigationViewController`, not `MyEventDetailsViewController`. Probably [this answer](http://stackoverflow.com/questions/8429088/ios-storyboard-passing-data-navigationviewcontroller) can help you fixing it. – Antonio Feb 12 '15 at 22:01
  • worked brilliantly - thanks Antonio....of course now I have to work on this: fatal error: unexpectedly found nil while unwrapping an Optional value – Mike Gibbs Feb 12 '15 at 22:08
  • Cool - one step at a time :) I've updated my answer to include the solution. – Antonio Feb 12 '15 at 22:24