2015-02-18 8 views
11

Ich versuche, einen Alarm-Controller, der ein Textfeld hat aufrufen. Aber ich möchte sofort das Nummernblock zeigen, da der Benutzer nur Zahlen eingeben sollte. Ich habe mit dem folgenden versucht, aber es funktioniert nicht.Wie ein Nummernblock in einem Textfeld eines Alarm-Controllers [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) 

Antwort

20

Selbst gefunden! Es könnte für jemand anderen nützlich sein.

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

Dies sollte nun 'UIKeyboardType.numberPad' –

3

Swift 3:

alert.addTextField { (textField) in 
    textField.keyboardType = .decimalPad 
} 
Verwandte Themen