swiftkeyboarduitextfielduialertviewnumpad

unopened numeric and other keyboard on textfield alertView in swift


I have an edit button and when I touch that button it shows an alert view. The alert view has a text field. I customized that keyboard to "numpad". But it never change to numpad only show default keyboard. How can I do it? The code is below. Also I have tried textfield.keyboardType = .numpad in view didload and editbuttonclicked func

@IBAction func editQuantityButtonClicked(_ sender: Any) {
    
    var textField = UITextField()
    
    
    let alert = UIAlertController(title: "", message: "Enter a quantity", preferredStyle: .alert)
    
    
    let action = UIAlertAction(title: "Ok", style: .default) { (anAction) in
        if let aText = textField.text{
            self.pickerDataLabel.text = "\(aText) mL"
        }
    }
    alert.addAction(action)
    self.present(alert, animated: true, completion: nil)
            
    alert.addTextField { (aText) in
        textField = aText
    }
}

Solution

  • Add keyboard type here.

    alert.addTextField { (aText) in
        aText.keyboardType = .numberPad //<-- Here
        textField = aText
    }