-1

I'm wanting to change my label "teamNameLabel" whenever a user inputs text into my textfield from AlertController and hit's the ENTER button.

I'm really close. I just need the text once entered to display as my label when the hit the ENTER button.

ALSO: Is there anyway to make the text entered into my textfield CAPITALS ONLY? (eg. User has to enter "TEAM NAME" and not "team name")

See code and images below:

@IBAction func editTeamNameButton() {

    let message = "Please Enter Below"
    let alert = UIAlertController(title: "ENTER TEAM NAME", message: message, preferredStyle: .alert)

    let action = UIAlertAction(title: "ENTER", style: .default, handler: nil)


    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)


    alert.addAction(action)
    alert.addAction(cancelAction)
    alert.preferredAction = action


    alert.addTextField(configurationHandler: { (textField) in
        textField.placeholder = "Eg. TEAM NAME"



    })

    present(alert, animated: true, completion: nil)

    }

Click Here To View Screenshot Of My AlertController

J.Kearney
  • 7
  • 1
  • 7
  • https://stackoverflow.com/questions/34504317/how-to-add-textfield-in-uialertcontroller It's the `let textField = alertController.textFields?.first` part that allow to retrieve the content of the textfield. – Larme Dec 17 '18 at 14:50

1 Answers1

0

You have to add handler for your UIAlertAction and in this handler you can check if first UITextField in alert's textFields exists, and if exists, you can assign text property of your label as text property of this textField

let action = UIAlertAction(title: "ENTER", style: .default) { _ in
    if let textField = alert.textFields?[0] {
        self.teamNameLabel.text = textField.text!
    }
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40