0

I'm having a table view that contains this prepareForSegue function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController
    let myindexpath = self.MainPageTableView.indexPathForSelectedRow
    let titleString = self.CategoryTitle.object(at: ((myindexpath as NSIndexPath?)?.row)!) as? String
    upcoming.titlestring = titleString
    self.MainPageTableView.deselectRow(at: myindexpath!, animated: true)
}

To get the title from 6 cells and save them to next view in nvigational bar title.

In this view that contains the prepare for segue function there's a button in navigational bar that moves to another (third) view controller!

Whenever I click on that button the app crashes on this line:

let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController

I know the reason of the error but how can I solve it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mariah
  • 573
  • 3
  • 10
  • 26
  • What is the reason of the error which you know? And what the logs says about your crash? – Santosh Sep 21 '16 at 18:44
  • The upcoming should be CategoryDeviceViewController but when clicking on the button it opened the third view controller – Mariah Sep 21 '16 at 18:49
  • Did you set `destination` viewController properly in your storyboard for this `segue`? – Santosh Sep 21 '16 at 18:50
  • Use `if (segue.identifier == "upcoming1")` and load accordingly. – Santosh Sep 21 '16 at 18:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123885/discussion-between-santosh-and-mariah). – Santosh Sep 21 '16 at 18:53
  • Use `if (segue.identifier == "upcoming1")` and load accordingly. And in don't forget to set `upcoming1` in storyboard as `segue identifier` – Santosh Sep 21 '16 at 18:53
  • Can you explain it more in the answer? First did you get exactly what I mean? – Mariah Sep 21 '16 at 18:53
  • 1
    Almost definitely segue.destination is not a CategoryDeviceViewController so the force cast is failing. Set a breakpoint and see what type of object segue.destination is. – David S. Sep 21 '16 at 18:57
  • @ David Shaw Thanks for the note. – Mariah Sep 21 '16 at 19:52

1 Answers1

1

As per your segues and crash logs, here is you are missing i guess. Try to identify the destination with your segue identifiers:

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowCategoryDevice" {
        let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController
        let myindexpath = self.MainPageTableView.indexPathForSelectedRow
        let titleString = self.CategoryTitle.object(at: ((myindexpath as NSIndexPath?)?.row)!) as? String
        upcoming.titlestring = titleString
        self.MainPageTableView.deselectRow(at: myindexpath!, animated: true)

    }else  {
        let upcoming: PopOverViewController = segue.destination as! PopOverViewController
        //do rest of your stuff
    }
}
Santosh
  • 2,900
  • 1
  • 16
  • 16
  • Can you help me with this question: http://stackoverflow.com/questions/39619860/getting-the-values-of-data-stored-in-firebase/39620712#39620712 – Mariah Sep 21 '16 at 20:08