swift - 如何限制 UIAlertController 上的 UITextField 仅采用指定范围的数字?

标签 swift

我创建了一个带有 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/

相关文章:

ios - 日志 - DispatchQueue - self.tableView.reloadData() 未被调用

SwiftUI - 日期选择器未正确显示

ios - 如何在其他项目中复用 Swift 代码?

ios - 在自定义 View 中定义点击手势时出现无法识别的选择器错误 [Swift]

ios - 如何以编程方式将不同单元格的不同图像添加到 tableView (Swift 3)

ios - 为什么 UITableViewCell 在滚动可用单元格后显示错误数据?

UITabBarItem 中的 iOS 自定义动画

使用 CollectionType 的 Swift 协议(protocol)和协议(protocol)扩展

ios - 当我更改约束的优先级时,更改不会使用 UpdateconstraintIfNeeded 进行动画处理

ios - 不从缓存中获取对象 - 解析