Good day. I'm creating my first own app and have ran into an issue. I have a AR scene with clickable stuff which when you touch them a segue triggers into a ViewController and sets that view controllers labels and textview depending on what was touch on screen.
Explanations: 1. CaseViewController is the target view controller. 2. the "artNews" and "politicalNews" are string arrays in which I've written 3 strings, they are defined and are never nil.
Question: I get a crash due to the segueInputText being nil. Why does it become nil and how do I correct it?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! CaseViewController
destinationVC.segueInputText = sender as? [String]
print("\(String(describing: sender))")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touchLocation = touches.first?.location(in: sceneView),
let hitNode = sceneView?.hitTest(touchLocation, options: nil).first?.node,
let nodeName = hitNode.name
else { return }
if nodeName == imageNameArray[0] {
performSegue(withIdentifier: "CaseViewController", sender: artNews)
} else {
print("Found no node connected to \(nodeName)")
return
}
if nodeName == imageNameArray[1] {
performSegue(withIdentifier: "CaseViewController", sender: politicalNews)
} else {
print("Found no node connected to \(nodeName)")
return
}
the CaseViewController has UILabels and UITextViews connected and this:
var segueInputText : [String]? {
didSet {
setupText()
}
}
func setupText() {
// Why are these values nil?
testLabel.text = segueInputText![0]
ingressLabel.text = segueInputText![1]
breadLabel.text = segueInputText![2]
testLabel.reloadInputViews()
ingressLabel.reloadInputViews()
breadLabel.reloadInputViews() //breadLabel is a UITextView
}
Thank you for reading my question! Kind regards.
Don't use the didSet
observer in this case. It will never work.
In setupText()
IBOutlets
are accessed which are not connected yet at the moment prepare(for
is called.
Remove the observer
var segueInputText : [String]?
and call setupText
in viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupText()
}