Mariah Mariah - 2 months ago 17
iOS Question

TableView and and Button (Crash)

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?

Answer

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
    }
}