skip to Main Content

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


  1. try this 
    guard let EndConfiguration = self.storyboard?.instantiateViewController(withIdentifier: EndConfiguration) as? EndConfiguration else
            {
                fatalError("Impossible d'acceder correctement au cellules des alarmes")
            }
    _ = EndConfiguration.view
    EndConfiguration.ModeleOutlet.text = Alarme.shared.Alarmes[indexPath.row].Modele
    
    Login or Signup to reply.
  2. I am making the assumption that type of ModeleOutlet is a Label 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.

    class EndConfiguration: UIViewController {
        public var sharedText: String?
    
        @IBOutlet var ModeleOutlet: UILabel?
    
        func viewDidLoad() {
            super.viewDidLoad()
    
            self.ModeleOutlet?.text = sharedText
        }
    
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search