1

I'm new to Swift and to iOS Development. I currently have 2 ViewControllers, a button in the first and a label in the second one. I've connected the first button to the second ViewController and the transition works.

Now, when I try changing the label's text I get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

.

Here you find my prepare function in the first ViewController:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "mySegue" {
            let vc = segue.destination as! SecondViewController
            vc.secondResultLabel.text = "Testing"
         }
    }

Can it be that the label in the second ViewController is somehow protected ?

Thanks for the help

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
fbelfort
  • 283
  • 1
  • 4
  • 12
  • This looks similar: http://stackoverflow.com/questions/39887587/error-found-nil-while-unwrapping-an-optional-value-while-pass-data-to-the-new/39887622#39887622 – Bista Nov 23 '16 at 13:03

4 Answers4

5

You need to pass the String to the SecondViewController instead of directing setting it, as the UILabel has not been created yet.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" {
        let vc = segue.destination as! SecondViewController
        vc.secondResultLabelText = "Testing"
    }
}

And in your SecondViewController viewDidLoad method set the UILabel to be the string

var secondResultLabelText : String!

override func viewDidLoad() {

    secondResultLabelText.text = secondResultLabelText
}
Rikh
  • 4,078
  • 3
  • 15
  • 35
3

add a string variable in the second view controller

var labelText: String!

in second view controller also (in viewDidLoad)

self.secondResultLabel.text = self.labelText

then first view controller prepare for segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" {
        let vc = segue.destination as! SecondViewController
        vc.labelText = "Testing"
     }
}

this is because second view controller's UILabel Outlet is not being initialized yet in prepare for segue

Rikh's answer is the same, both his answer and mine are the same

Sameh Salama
  • 765
  • 1
  • 7
  • 17
2

Welcome aboard :)

Your problem is that your SecondViewController, and more specifically vc.secondResultLabelText is not initiated when you call prepare, so secondResultLabel is actually nil at that time.

You need to add a variable to your SecondViewController like so:

var labelText: String = ""

And then set that value instead:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" {
        let vc = segue.destination as! SecondViewController
        vc.labelText = "Testing"
     }
}

In viewWillAppear or viewDidLoad on your SecondViewController you can then use that value for your secondResultLabelText which is now ready, connected, and won't crash

secondResultLabelText.text = labelText

Hope that helps.

pbodsk
  • 6,787
  • 3
  • 21
  • 51
0

First take a global variable in SecondViewController... eg I took "secondViewControllerVariable". Then get the text you want to display in your SecondViewController.

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

    {
        if segue.identifier == "mySegue" 
            {
                let vc = segue.destination as! SecondViewController
                vc.secondViewControllerVariable = "Your string you get in FirstViewController"
            }
    }

And then in your SecondViewController, in viewDidLoad method set the UILabel to be the string

   var secondViewControllerVariable : String! // You have to declare this first in your SecondViewController Globally 

   override func viewDidLoad() 
     {
          vc.secondResultLabelText.text = secondViewControllerVariable
     }

That's it. Happy Coding.

sandeep nag
  • 148
  • 8