2015-02-18 11 views
11

Sto provando a chiamare un controller di avviso che ha un campo di testo. Ma mi piacerebbe mostrare immediatamente il tastierino numerico dal momento che l'utente dovrebbe inserire solo i numeri. Ho provato con il seguente ma non funziona.Come avere un tastierino numerico in un campo di testo di un controller di allerta [swift]

let alert = UIAlertController(title: "New Rating", message: "Rate this!", preferredStyle: UIAlertControllerStyle.Alert) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in }) 

    let saveAction = UIAlertAction(title: "Save", style: .Default, handler: { (action: UIAlertAction!) in 

    let textField = alert.textFields![0] as UITextField 
    textField.keyboardType = UIKeyboardType.NumberPad 

    self.updateRating(textField.text) 
    }) 

    alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) in } 

    alert.addAction(cancelAction) 
    alert.addAction(saveAction) 

    self.presentViewController(alert, animated: true, completion: nil) 

risposta

20

Trovato per conto mio! Potrebbe essere utile per qualcun altro.

alert.addTextField(configurationHandler: { textField in 
    textField.keyboardType = .numberPad 
}) 
+0

Questo dovrebbe essere 'UIKeyboardType.numberPad' –

3

Swift 3:

alert.addTextField { (textField) in 
    textField.keyboardType = .decimalPad 
} 
Problemi correlati