Problems with unwinding a segue

29 Views Asked by At

Having a real issue with this.

I have now managed to transfer my label data across a segue and display it on a new view controller. Thank you to all of those who helped with this. For this I used the following code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let BMI_Result = segue.destination as! BMI_Result
    
    if let text = result3Label.text {
        BMI_Result.text = text
    }

The problem I am having now is that when I try to unwind segue using a back button from this view, the app crashes and I get the following error:

  • Thread 1: signal SIGABRT

The as! command in the code has a red underline.

I have read up on the signal SIGABRT error and from what I can see, it's a generic error for that line of code. I presume it is because this code is linked to another segue but I don't know how to correct it.

Anyone got any ideas?

I have attempted to redo the back segue and link the button to the contents page. None of this has worked.

1

There are 1 best solutions below

1
burnsi On BEST ANSWER

The issue here is the force cast as!. As you go back your destination has changed.

Change this to an optional cast and guard against it.

guard let BMI_Result = segue.destination as? BMI_Result else{
    return
}

if let text = result3Label.text {
    BMI_Result.text = text
}