我创建了一个带有 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
}))
最佳答案
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
}
}))
关于swift - 如何限制 UIAlertController 上的 UITextField 仅采用指定范围的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804703/