Display Alert with UITextField Entry in Swift (XCODE 8.3)

Last Updated: October 24, 2017

In your iOS application you often have to display alert window (UIAlertController) with TextField on it.

Simulator Screen Shot Oct 24 2017 6.57.04 AM

      let uiAlertControl = UIAlertController(title: "Number", message: "Please Enter the Number", preferredStyle: .alert)
      let uiAlertAction = UIAlertAction(title: "Confirm", style: .destructive) { (_) in
        if let textField = uiAlertControl.textFields?[0]{
      uiAlertControl.addTextField(configurationHandler: nil)
      uiAlertControl.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
      self.present(uiAlertControl, animated: true, completion: nil)

First you create UIAlertController object and then you can create UIAlertAction object which can be added to UIAlertController object

To add TextField to UIAlertController you can use
uiAlertControl.addTextField(configurationHandler: nil)