Search code examples
iosswiftuitableviewuistoryboardsegue

a simple performSegue throws and error in prepare for segue func


Maybe something silly, but I am trying to simply move from a tableviewController to a viewController not passing any data just a simple move using

@IBAction func requestPanel(_ sender: Any) {

   performSegue(withIdentifier: "sendMail", sender: AnyObject)
}

And then I get an error in a prepare for segue func.

But what does that have to no with my other segue?

I understand that that segue will have trouble since there is no data.

As you see here segue is above prepare for segue.

@IBAction func requestPanel(_ sender: Any) {

    self.performSegue(withIdentifier: "sendMail", sender: AnyObject.self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var indexPath: IndexPath = self.tableView.indexPathForSelectedRow!
    let desti = segue.destination as! DeviceDetailTableViewController

    let selectedRecord = onlineDevices[indexPath.row]

    let panelWidth = selectedRecord.object(forKey: "panelwidth") as? String
    let panelHight = selectedRecord.object(forKey: "panelhight") as? String
    let panelPitch = selectedRecord.object(forKey: "panelpitch") as? String
    let panelPower = selectedRecord.object(forKey: "panelpower") as? String
    let panelWeight = selectedRecord.object(forKey: "panelweight") as? String
    let panelMaker = selectedRecord.object(forKey: "maker") as? String
    let panelModel = selectedRecord.object(forKey: "model") as?      String

    desti.pawidth = panelWidth!
    desti.pahight = panelHight!
    desti.papitch = panelPitch!
    desti.papower = panelPower!
    desti.weight = panelWeight!
    desti.maker = panelMaker!
    desti.model = panelModel!
}



 override func numberOfSections(in tableView: UITableView) -> Int {
 return 1
 }
 override func tableView(_ tableView: UITableView,   numberOfRowsInSection section: Int) -> Int {
 return onlineDevices.count
 }
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceCell", for: indexPath)

    // Configure the cell...

    let noteRecord: CKRecord = onlineDevices[(indexPath as IndexPath).row]

    cell.textLabel?.text = noteRecord.value(forKey: "maker") as? String
    cell.detailTextLabel?.text = noteRecord.value(forKey: "model") as? String


    return cell
}


}

Solution

  • All segues in a viewController will go through the same prepare(for:sender:) method. You need to use the segue.identifier to handle the different segues differently.

    Your sendMail segue comes from a UIBarButton action instead of from a selected row, so check for that segue and handle it separately:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "sendMail" {
            // nothing extra to be done here
        } else {
            // do your normal path here
            if let indexPath = self.tableView.indexPathForSelectedRow {
                // you have a valid selected row so proceed
            }
        }
    }