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?

Velocity
  • 169
  • 2
  • 8
  • Is your outlet connected? – Kreiri Aug 21 '14 at 09:18
  • Yes, it is (as mentioned in the first post) What was the minus for? – Velocity Aug 21 '14 at 09:19
  • possible duplicate of [Swift: PrepareForSegue, Swift Cast failure](http://stackoverflow.com/questions/25406309/swift-prepareforsegue-swift-cast-failure) – Antonio Aug 21 '14 at 09:22
  • In the debugger enter po vc.selectedBundesland – Paulw11 Aug 21 '14 at 09:42
  • "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 segue – Velocity Aug 21 '14 at 09:59
  • This indicates that your IBoutlet *isn't* mapped correctly (I am assuming that selectedBundesland is a UITextField in your storyboard – Paulw11 Aug 21 '14 at 11:48
  • [Link to dropbox!](https://www.dropbox.com/sh/k1c1kwaalcdfjfg/AADtxZUVexnuLST0VZXndRUva) 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? – Velocity Aug 21 '14 at 12:33
  • 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 worked – Paulw11 Aug 21 '14 at 13:17
  • I still get a crash on the "backToCalculator" segue, although you would normally do this with an unwind rather than pushing the same view controller – Paulw11 Aug 21 '14 at 13:29
  • 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 workaround – Velocity Aug 21 '14 at 13:43
  • It is late here and I am not thinking straight - see my answer – Paulw11 Aug 21 '14 at 14:18

5 Answers5

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.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
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"
}
BeomGeun Lee
  • 121
  • 1
  • 2
  • 3
    I get "fatal error: unexpectedly found nil while unwrapping an Optional value" when doing this via vc.textValue = "Test". Any idea? – SleepNot Apr 23 '15 at 08:44
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!

Josh
  • 529
  • 6
  • 21
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

Eran Artzi
  • 11
  • 2
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

Laszlo
  • 2,803
  • 2
  • 28
  • 33