(Swift) PrepareForSegue: fatal error: unexpectedly found nil while unwrapping an Optional value
Asked Answered
S

5

9

DetailViewController:

    @IBOutlet var selectedBundesland: UILabel!

TableViewController:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {


    if (segue.identifier == "BackToCalculator") {
        var vc:FirstViewController = segue.destinationViewController as FirstViewController
            vc.selectedBundesland.text = "Test"
    }

IBOutlet is connected!

Error: fatal error: unexpectedly found nil while unwrapping an Optional value

I read multiple pages about Optionals but i didn't know the answer to my problem.

Do you need more information about my project?

Selfdeceit answered 21/8, 2014 at 9:16 Comment(11)
Is your outlet connected?Calipee
Yes, it is (as mentioned in the first post) What was the minus for?Selfdeceit
possible duplicate of Swift: PrepareForSegue, Swift Cast failureReciprocity
In the debugger enter po vc.selectedBundeslandAugur
"nil" I need to know how to give it a value. It also does not work when assigning a value (e.g selectedBundesland.text = "Test") before pushing the segueSelfdeceit
This indicates that your IBoutlet isn't mapped correctly (I am assuming that selectedBundesland is a UITextField in your storyboardAugur
Link to dropbox! i created a completly new program just to fix this error. the same error occured again. could someone try to help by watching the code?Selfdeceit
I downloaded the project. Your label isn't connected to your IBOutlet. For some reason I was unable to connect it to the existing IBOutlet, but if I switched to the assistant editor I could drag from the label to the class and make a new IBOutlet with the same name. I then deleted the original and it workedAugur
I still get a crash on the "backToCalculator" segue, although you would normally do this with an unwind rather than pushing the same view controllerAugur
ah ok.. thanks for your help. i will read about an unwind. IIRC i need an Objective-C-Header file to implement it. Edit: Since beta 4 of Xcode 6 it is working without the workaroundSelfdeceit
It is late here and I am not thinking straight - see my answerAugur
A
34

You cannot write directly to the UILabel in prepareForSegue because the view controller is not fully initialised yet. You need to create another string property to hold the value and put it into the label in the appropriate function - such as viewWillAppear.

Augur answered 21/8, 2014 at 14:18 Comment(0)
A
12

DetailViewController:

var textValue: String = ""
@IBOutlet weak var selectedBundesland: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    selectedBundesland.text = textValue
}

TableViewController:

     if (segue.identifier == "BackToCalculator") {  
         var vc:FirstViewController = segue.destinationViewController as FirstViewController
         vc.textValue = "Test"
}
Alfano answered 10/9, 2014 at 13:42 Comment(1)
I get "fatal error: unexpectedly found nil while unwrapping an Optional value" when doing this via vc.textValue = "Test". Any idea?Raymonraymond
W
5

Recently had this problem. The problem was that I had dragged the segue from a specific object from my current view controller to the destination view controller - do not do this if you want to pass values.

Instead drag it from the yellow block at the top of the window to the destination view controller. Then name the segue appropriately.

Then use the if (segue.identifier == "BackToCalculator") to assign the value as you are currently. All should work out!

Waiwaif answered 24/8, 2016 at 1:20 Comment(0)
M
1

I just had the same problem, I solved it by defining a string that is not connected to an outlet in the new view controller and than referring to it in the prepareForSegue() method, in the new VC I made the label outlet to take the value of the non connected string in the viewDidLoad() method.

Cheers

Mangum answered 22/4, 2016 at 1:19 Comment(0)
P
0

While the correct solution is to store the text and attach it to the label later in viewDidLoad or something, for testing proposes, you can bypass the issue by forcing the destinationViewController to build itself from storyboard by calling its view property like:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){

     if (segue.identifier == "TestViewController") {
          var vc:FirstViewController = segue.destination as! TestViewController
          print(vc.view)
          vc.testLabel.text = "Hello World!"
     }
}

made for Swift 3.0 with love

Pitt answered 5/4, 2017 at 22:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.