user2411290
user2411290

Reputation: 641

Swift: UITextField is not being recognized

I want the user to press a button, and then for them to be able to see an alert where they can enter an input (to set a price for a service). The other logic involves saving data to a database, which is not really relevant to my problem.

I am using the following example:

https://stackoverflow.com/a/30139623/2411290

It definitely works, in that it shows the alert correctly, but once I include

print("Amount: \(self.tField.text)")

"self.tField.text" is not recognized. The specific error I get is:

value of type 'testVC' has no member 'tField'

@IBAction func setAmount(_ sender: Any) {

    var tField: UITextField!


    func configurationTextField(textField: UITextField!)
    {
        print("generating textField")
        textField.placeholder = "Enter amount"
        tField = textField

    }

    func handleCancel(alertView: UIAlertAction!)
    {
        print("Cancelled")
    }

    let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)

    alert.addTextField(configurationHandler: configurationTextField)
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:handleCancel))
    alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
        print("Done !!")

    }))
    self.present(alert, animated: true, completion: {
        print("completion block")
        print("Amount: \(self.tField.text)") // Error here


    })

    //// other logic for app
}

Upvotes: 0

Views: 398

Answers (2)

My guess is that when you present an alert your current ViewController is the alert viewController... And in your alert there is no variable tField.

On the exampled you quoted the alert was presented only after the print with tField's value. That why that worked there and doesn't work in your case.

Upvotes: -1

rmaddy
rmaddy

Reputation: 318774

tField is a local variable inside your setAmount function. It is not a property of the class.

Change:

self.tField.text

to:

tField.text

That will allow you to access the local variable.

But the real question is why are you creating a local variable of UITextField inside this function? Why are you printing its text when the text field isn't used anywhere?

Most likely you should be accessing the alert's text field inside the action handler for the "Done" button. There's no need to do anything inside the completion block of presenting the alert.

@IBAction func setAmount(_ sender: Any) {
    let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)

    alert.addTextField(configurationHandler: { (textField) in
        print("generating textField")
        textField.placeholder = "Enter amount"
    })

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action) in
        print("Cancelled")
    })

    alert.addAction(UIAlertAction(title: "Done", style: .default) { (action) in
        print("Done !!")
        if let textField = alert.textFields?.first {
            print("Amount: \(textField.text)")
        }
    })

    self.present(alert, animated: true, completion: nil)
}

Upvotes: 2

Related Questions