this should do the trick:
Make your viewController compatible with UITextFieldDelegate
assign popup.textFields![0].delegate self
add a unique tag to popup.textFields![0] (I used 999 in the example below)
realize it
func textFieldShouldBeginEditing(textField: UITextField) -> Bool { if textField.tag == 999 { textField.tag = 0 return false }else{ return true } }
your code should look like this:
let popup = UIAlertController(title: "My title", message: "My message", preferredStyle: .Alert) popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in optionalTextField.placeholder = "This is optional" } popup.textFields![0].delegate = self popup.textFields![0].tag = 999 let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in let optionalTextField = popup.textFields![0] let text = optionalTextField.text print(text) } let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) popup.addAction(cancelAction) popup.addAction(submitAction) self.presentViewController(popup, animated: true, completion: nil)
source share