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
}
}
The error message is clear. You are saying tableView.indexPathForSelectedRow
at a time when no row is selected. Therefore that value is nil
, and you crash when you force-unwrap it.
You wrote your original prepareForSegue
when your only segue was triggered by the user selecting a row of the table. But this segue is not triggered by the user selecting a row of the table; it is triggered by the user tapping a button. You have to account for that difference.
(This is a major flaw in the whole architecture: all segues from a given view controller flow into the same prepareForSegue
, which becomes a bottleneck.)
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.