Ich habe diesen Beispielcode über gefunden, wie das Textfeld für die UIAlertController
hinzufügen, aber ich bin nicht in der Lage die die Benutzer beschränken nur 10 Zeichen als Grenze ich eine Funktion eingeben, um das letzte Zeichen, wenn es zu entfernen ist vorbei, wenn Bearbeitung für ein normales Textfeld geändert, aber nicht in der Lage, das Textfeld auf der UIAlertController
zu überprüfen.Beschränken Sie die Eingabe eines Textfelds in UIAlertController, wenn der Benutzer mit swift?
//Function to Remove the last character
func trimStr(existStr:String)->String{
var countExistTextChar = countElements(existStr)
if countExistTextChar > 10 {
var newStr = ""
newStr = existStr.substringToIndex(advance(existStr.startIndex,(countExistTextChar-1)))
return newStr
}else{
return existStr
}
}
var inputTextField: UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in
}
actionSheetController.addAction(nextAction)
//Add a text field
**actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
inputTextField = textField**
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
Vielen Dank.