我已经在其上创建了一个带有UITextField的UIAlertController,并且UITextField只需要在0到100之间取整数。
我该怎么做?
这是我的代码
let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert)
confirm.addTextField(configurationHandler: { (textField) in
textField.placeholder = "Please fill the number between 0-100"
})
confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
confirm.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive, handler: { (action) in
self.isReserve = true
self.reservedIcon.alpha = 0
self.reservedTitle.alpha = 0
self.cancelReservedIcon.alpha = 1
self.cancelReservedTitle.alpha = 1
}))
答案 0 :(得分:0)
let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert)
confirm.addTextField(configurationHandler: { (textField) in
textField.placeholder = "Please fill the number between 0-100"
})
confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
confirm.addAction(UIAlertAction(title: "Yes", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0]
if textField.text != "1234" { // change your if condition
self.isReserve = true
self.reservedIcon.alpha = 0
self.reservedTitle.alpha = 0
self.cancelReservedIcon.alpha = 1
self.cancelReservedTitle.alpha = 1
} else {
// your else condition
}
}))
答案 1 :(得分:0)
将UITextFieldDelegate
添加到您的控制器
然后你的代码:
let confirm = UIAlertController(title: "I want to reserve", message: "sometext", preferredStyle: UIAlertControllerStyle.alert)
confirm.addTextField(configurationHandler: { (textField) in
textField.placeholder = "Please fill the number between 0-100"
})
let textfield = confirm.textFields?.first
textfield?.keyboardType = .numberPad
textfield?.delegate = self
confirm.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
confirm.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive, handler: { (action) in
self.isReserve = true
self.reservedIcon.alpha = 0
self.reservedTitle.alpha = 0
self.cancelReservedIcon.alpha = 1
self.cancelReservedTitle.alpha = 1
}))
self.present(confirm, animated: true) {
}
添加Textfield委托:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
return text.characters.count <= 100 // Your limit
}
答案 2 :(得分:0)
您可以使用shouldChangeCharactersInRange方法和String扩展来检查输入字符串是否包含数字并控制0到100之间的文本输入
extension String {
var isNumber : Bool {
get{
return !self.isEmpty && self.stringWithoutWhitespaces.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
}
}
var stringWithoutWhitespaces: String {
return self.replacingOccurrences(of: " ", with: "")
}
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// return true if the string only contains numeric characters
var isValid = false
if string.isNumber {
if let num = Int(string.stringWithoutWhitespaces) {
isValid = (num >= 0 && num <= 100)
}
}
return isValid
}