【发布时间】:2016-05-09 15:36:16
【问题描述】:
我试图阻止 UITextField 输入空格字符或不输入字符,但仅适用于不输入字符。我需要的是:不允许用户在不输入任何字符和空格的情况下保存文本字段;但用户可以输入,例如:“新类别”。所以空格只允许在字母或数字之间,而不仅仅是空格。
这是我的代码:
@IBAction func btnAddCategory(sender: AnyObject) {
let alert = UIAlertController(title: "Ajouter une catégorie", message: nil, preferredStyle: .Alert)
alert.view.tintColor = Utils.colorFromHex(0x585858)
let confirmAction = UIAlertAction(title: "OK", style: .Default, handler: ({ (_) in
if let field = alert.textFields?[0] {
if field.text! == NSCharacterSet.whitespaceCharacterSet() || field.text! == ""{
self.displayAlert("Attention", alertMsg: "Vous ne pouvez pas créer des données vides")
} else {
if self.checkDuplicates(field.text!) {
self.displayAlert("Attetion", alertMsg: "Vous avez déjà une catégorie avec ce nom !")
} else {
self.saveCategory(field.text!)
self.tableView.reloadData()
}
}
}
}
))
let cancelAction = UIAlertAction(title: "Annuler", style: .Cancel, handler: nil)
alert.addTextFieldWithConfigurationHandler({(textField) in
textField.placeholder = "Titre"
textField.font = UIFont(name: "Roboto-Light", size: 15)!
})
alert.addAction(confirmAction)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
那么有人可以帮我解决这个问题吗?
【问题讨论】:
标签: swift uitextfield ios9