1

I am adding textField to UIAlertController with the code below:

let anAlertController = UIAlertController(title:title, message:message, preferredStyle: .alert)
        weak var weakSelf = self

        let aDefaultAction = UIAlertAction(title:defaultTitle, style: UIAlertAction.Style.default) {
            UIAlertAction in

        anAlertController.addAction(aDefaultAction)
    }

    let aCancelAction = UIAlertAction(title:canceltTitle, style: UIAlertAction.Style.cancel) {
        UIAlertAction in
    }
    anAlertController.addAction(aCancelAction)

    anAlertController.addTextField { (textField) in
        textField.textAlignment = NSTextAlignment.center
        textField.text = "textFieldText"
        textField.delegate = self
        textField.resignFirstResponder()
    }
    self.present(anAlertController, animated: true, completion: nil)

When i try to present UIAlertController, textfield becoming first responder automatically. I don’t want textfield should become first responder while presenting alert. i Googled and tried several ways to avoid this but no luck. Can anybody tell how to achieve this?

DJ-Glock
  • 1,277
  • 2
  • 12
  • 39
Logger
  • 1,274
  • 1
  • 14
  • 25

2 Answers2

5

try this

anAlertController.addTextField { (textField) in
        textField.textAlignment = .center
        textField.text = "textFieldText"
        textField.delegate = self as? UITextFieldDelegate
        textField.isEnabled = false

    }
    self.present(anAlertController, animated: false, completion: {
        if let getTextfield  = anAlertController.textFields?.first{
                getTextfield.resignFirstResponder()
                getTextfield.isEnabled = true
        }

    })
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • you will see keyboard while alert appears – Gralex Feb 06 '19 at 09:45
  • @Anbu.karhtik. I tried this but it shows keyboard for a while then dis appear. it wont work – Logger Feb 06 '19 at 09:48
  • @Gralex - yes, give the moment I try to resolve this – Anbu.Karthik Feb 06 '19 at 09:48
  • @lazyCoder - check the updated answer Gralex - thanks for reference – Anbu.Karthik Feb 06 '19 at 09:59
  • @Anbu.Karthik Thanks its working fine, but when i touch the textfield keyboard popsup now. How to stop keyboard popup on textfield touch but touch enabled – Logger Feb 06 '19 at 10:13
  • @lazyCoder delegate can implement method `textFieldShouldBeginEditing` with `false` result. No need to enable\disable textFiled in this case. – Gralex Feb 06 '19 at 10:31
  • @Gralex i need to allow user touch as per my requirement. Because i want to allow user to copy textfield text but same time i dont want key keyboard to appear. Can you please help me to achieve this requirement. – Logger Feb 06 '19 at 10:33
  • @lazyCoder - give the moment – Anbu.Karthik Feb 06 '19 at 10:36
  • 1
    @lazyCoder - check this for ref : https://stackoverflow.com/questions/1920541/enable-copy-and-paste-on-uitextfield-without-making-it-editable – Anbu.Karthik Feb 06 '19 at 11:04
  • @Anbu.Karthik with above link i am able to prevent keyboard on textfield touch and allow it to copy. Thank you so much for your time and quick response. – Logger Feb 06 '19 at 11:18
0

You can disable UITexField for a while, it can't be responder on alert appear:

anAlertController.addTextField { (textField) in
    textField.textAlignment = NSTextAlignment.center
    textField.text = "textFieldText"
    textField.isEnabled = false
    DispatchQueue.main.async { textField.isEnabled = true }
}
Gralex
  • 4,285
  • 7
  • 26
  • 47