gpt4 book ai didi

ios - 检查 UIAlertController 中的字段验证

转载 作者:行者123 更新时间:2023-11-30 13:57:53 25 4
gpt4 key购买 nike

我有带有输入字段的UIAlertController。我想检查该字段是否不为空以及字段文本是否为数字。但我不明白如何在警报窗口中显示验证消息。

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

关于ios - 检查 UIAlertController 中的字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com