0

facing a nil exception while passing the property of text of a label on the FirstVC and reflecting it to the SecondVC on a Label aswell.

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

https://github.com/marlhex/PasingDataAroundVC

Debugger Desc.

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

        if segue.identifier == "toSecondSegue" {

            // Instance to the next screen
            let svc = segue.destination as! SecondVC

            // Assigning the same value to the next screen
            svc.sameName!.text = self.originalName.text
        }
    }

Does anyone knows how to pass the data accordingly?

I know the default value of the property of text of the label will be nil, but I am just stopped by this, I know, incredible

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Marlhex
  • 1,870
  • 19
  • 27
  • When reviewing the duplicate, see https://stackoverflow.com/a/49818897/1226963 for an answer that directly covers your specific issue. – rmaddy Feb 26 '19 at 17:47
  • @rmaddy can we also consider it as a duplicate of: https://stackoverflow.com/questions/28741399/iboutlet-of-another-view-controller-is? In addition, I might mention that `text` could be declared as a *property observer* to edit the value of the label text... – Ahmad F Feb 26 '19 at 17:59
  • @AhmadF Yes, that is also a duplicate. – rmaddy Feb 26 '19 at 18:08

4 Answers4

1

Why not use delegates?

protocol firstVCDelegate {
    func passingName(_ text: String)
}

class FirstVC: UIViewController {
    public weak var delegate: firstVCDelegate?

    func goToSecondVC() {
       self.delegate?.passingName(futureName.text)
    }

}

class SecondVC: UIViewController, firstVCDelegate {

    func passingName(_ text: String) {
      sameName.text = text
    }
}
Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
1

The label is nil until the vc loads , so

svc.sameName!.text = self.originalName.text

so using ! will crash the app , you need to declare a string inside the destination class like

@IBOutlet weak var sameName: UILabel!

var str = ""

override func viewDidLoad() {
    super.viewDidLoad()
    sameName.text = str   // assign here
}

then assign it in sender vc prepare

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

        if segue.identifier == "toSecondSegue" {

            // Instance to the next screen
            let svc = segue.destination as! SecondVC

            // Assigning the same value to the next screen 
            svc.str = self.originalName.text
        }
    }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

You are trying to pass text directly to a UILabel that is not loaded into the view when you try to assign the value.

You need to pass originalName.text to a String variable in the SecondVC, and then assign that value to the UILabel in viewDidLoad:

SecondVC

var name: String!

@IBOutlet weak var sameName: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    sameName.text = name
}

FirstVC

svc.name = self.originalName.text
rbaldwin
  • 4,581
  • 27
  • 38
1

You can't access the sub controller instance directly from FirstViewController. You can do this by using the String variable as following.

Declare temp String variable in SecondViewContorller and set that value to your label controller in viewDidLoad as following.

/// Temp string value to set over the controller
var tempValue: String = ""


override func viewDidLoad() {
    super.viewDidLoad()

    self.sameName.text = self.tempValue
}

Hope this will solve your issue.

Ayaz Rafai
  • 279
  • 2
  • 10