Variable written as nil when passed through segue

54 Views Asked by At

this is my code

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var madLibInput: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "completedSenteceSegue" {
            
            let nvc = segue.destination as! SecondViewController
            nvc.passedInput = madLibInput.text
        }
    }
}

the other viewcontroller is this:

import UIKit
class SecondViewController: UIViewController {
    @IBOutlet weak var myUncleWantsToText: UILabel!
    var passedInput: String!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myUncleWantsToText.text = "My uncle wants to go to the \(passedInput)" 
    }
}

The text ends up as "My uncle wants to go to the nil" regardless of input. Also it gives this warning: "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" I don't know why either of these are happening.

1

There are 1 best solutions below

0
Blazej SLEBODA On

Most likely, the segue identifier in the code does not match the "storyboard" one.