0

So I'm just learning the basics of Swift. I have a table view of items and when an item is clicked I want to show a view that displays the details of that specific item. I have the below code in which I'm trying to achieve this, although, I'm getting a Bad Instruction runtime error. Any idea where I'm going wrong? Thanks!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    super.prepare(for: segue,sender: sender)

    switch(segue.identifier ?? "") {
    //the add item button is pressed
    case "AddItem":
        os_log("Adding a new donation.", log: OSLog.default, type: .debug)

    //an existing item is pressed
    case "ShowDetailDrill":

        guard let itemViewController = segue.destination as? ItemViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedDonationItemCell = sender as? DonationItemCell else {
            fatalError("Unexpected sender: \(sender)")
        }

        guard let indexPath = tableView.indexPath(for: selectedDonationItemCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedDonation = donatedItems.getItem(index: indexPath.row)

        //TODO: load this to SubmissionVC
        itemViewController.donatedItem = selectedDonation

    default:
        fatalError("Unexpected Segue Identifier; \(segue.identifier)")
    }

}

enter image description here

Update

I forgot to mention that it breaks here

enter image description here

Update 2: Ok so I removed the navigation controller and changed the segue from a "present modally" to "show". It goes directly from the cell to the item controller. Everything now works, although I'm still somewhat confused on why it wasn't working with the navigation controller. If someone is able to explain the why, I'll mark that as the answer.

Sean Peterson
  • 368
  • 4
  • 15

1 Answers1

0

is the ShowDetailDrill segue's origin from the tableviewcell? if yes, try deleting it and then creating a new segue from the tableviewcontroller to ItemViewController instead.

meowmeowmeow
  • 713
  • 7
  • 17