Mahdi Appleseed
Mahdi Appleseed

Reputation: 79

Add a text field to UIAlertViewController in swift

i'm a new to swift and i have a UIAlertViewController and i want to add a text field to that and get the data from that. for example get the text inside the text field.

Upvotes: 0

Views: 366

Answers (1)

keithbhunter
keithbhunter

Reputation: 12324

Add a text field to the alert controller:

alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
    textField.placeholder = "Some Placeholder Text"
    // configure the text field here
}

Get the text from that text field when the user presses a button:

let okAction = UIAlertAction(title: "OK", style: .Cancel) { (action) -> Void in 
    let textField = alertController.textFields![0] as! UITextField
}
alertController.addAction(okAction)

Upvotes: 2

Related Questions