How can I auto-populate a Text Field once a button is selected?

I currently have a button and a text field, named offlineListing and productURLField respectively. I am trying to automatically input the text "N/A" into the productURLField once offlineListing has been selected by using the following syntax:

@IBOutlet weak var productURLField: UITextField!

@IBAction func offlineListing(_ sender: Any) {
    let productURLField = "N/A"
}

However once the button is selected, nothing happens. Is there a reason as to why this isn't working properly?

Answered by OOPer in 676691022

You declare a local constant within offlineListing(_:), but it is not used and the value is disposed at the end of the method.

(You would have seen a waring Initialization of immutable value 'productURLField' was never used; consider replacing with assignment to '_' or removing it, which should not be ignored.)

Even if the local constant and the IBOutlet have exactly the same identifier, they are two different things and have nothing to do with each other.

You may want to do something like this:

    @IBAction func offlineListing(_ sender: Any) {
        productURLField.text = "N/A"
    }
Accepted Answer

You declare a local constant within offlineListing(_:), but it is not used and the value is disposed at the end of the method.

(You would have seen a waring Initialization of immutable value 'productURLField' was never used; consider replacing with assignment to '_' or removing it, which should not be ignored.)

Even if the local constant and the IBOutlet have exactly the same identifier, they are two different things and have nothing to do with each other.

You may want to do something like this:

    @IBAction func offlineListing(_ sender: Any) {
        productURLField.text = "N/A"
    }
How can I auto-populate a Text Field once a button is selected?
 
 
Q