【发布时间】:2015-03-16 18:20:30
【问题描述】:
我找到了关于如何为UIAlertController 添加文本字段的示例代码但我无法限制用户只输入 10 个字符作为限制,当常规文本字段的编辑更改但无法检查UIAlertController上的文本字段时,我有一个删除最后一个字符的功能。
//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)
谢谢。
【问题讨论】:
标签: ios swift uialertcontroller