检查 UIAlertController 中的字段验证
Check field validation in UIAlertController
我有 UIAlertController
带输入字段。我想检查此字段是否不为空以及字段文本是否为数字。但我不明白如何在警报 window.
中显示验证消息
var inputTextField: UITextField?
let actionSheetController: UIAlertController = UIAlertController(title: "Enter price", message: "", preferredStyle: .Alert)
let nextAction: UIAlertAction = UIAlertAction(title: "Continiue", style: .Default) { action -> Void in
let priceField = actionSheetController.textFields![0] as UITextField
if let userPrice = priceField.text {
if userPrice == "" {
//RETURN AND SHOW SHOW ERROR MESSAGE, BUT KEEP ALERT OPEN
}
}
}
actionSheetController.addAction(nextAction)
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
inputTextField = textField
inputTextField!.placeholder = "Product price"
}
self.presentViewController(actionSheetController, animated: true, completion: nil)
检查字符串是否为十进制数:
let nonDigits = NSCharacterSet.decimalDigitCharacterSet().invertedSet
let isValidNumber : Bool = priceField.text?.rangeOfCharacterFromSet(nonDigits) == nil
我有 UIAlertController
带输入字段。我想检查此字段是否不为空以及字段文本是否为数字。但我不明白如何在警报 window.
var inputTextField: UITextField?
let actionSheetController: UIAlertController = UIAlertController(title: "Enter price", message: "", preferredStyle: .Alert)
let nextAction: UIAlertAction = UIAlertAction(title: "Continiue", style: .Default) { action -> Void in
let priceField = actionSheetController.textFields![0] as UITextField
if let userPrice = priceField.text {
if userPrice == "" {
//RETURN AND SHOW SHOW ERROR MESSAGE, BUT KEEP ALERT OPEN
}
}
}
actionSheetController.addAction(nextAction)
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
inputTextField = textField
inputTextField!.placeholder = "Product price"
}
self.presentViewController(actionSheetController, animated: true, completion: nil)
检查字符串是否为十进制数:
let nonDigits = NSCharacterSet.decimalDigitCharacterSet().invertedSet
let isValidNumber : Bool = priceField.text?.rangeOfCharacterFromSet(nonDigits) == nil