I’m coding an app on Xcode for IOS and I’d like to send a variable to another ViewController that hasn’t appeared yet. The problem is that when I make a variable of my future ViewController like this :
guard let EndConfiguration = self.storyboard?.instantiateViewController(withIdentifier: EndConfiguration) as? EndConfiguration else
{
fatalError("Impossible d'acceder correctement au cellules des alarmes")
}
And I’m trying to do this:
EndConfiguration.ModeleOutlet.text = Alarme.shared.Alarmes[indexPath.row].Modele
I get this error :
Fatal error: Unexpectedly found nil while implicitly unwrapping an
Optional value
I understood the Optionels but if I add a "?" after ModeleOutlet it will remove the error but the text remains unchanged in my other ViewController. I’m sure that the value I modify is full, don’t worry.
I’m replicating my problem because someone close it because there was already a solution when there wasn’t one at all.
Thank you in advance.
2
Answers
I am making the assumption that type of
ModeleOutlet
is aLabel
or of another type that may contain a text variable.If ModeleOutlet is indeed a label that is attached via storyboard, then I urge you to read up on the ViewController lifecycle. Here is a great article describing it : https://medium.com/good-morning-swift/ios-view-controller-life-cycle-2a0f02e74ff5
Now, to answer your question specifically, you’ll likely want to create a separate variable within your view controller then set the text variable within the function,
viewDidLoad
.