TextFieldValidator
此版本的 TextFieldValidator 用来验证 UITextFields,确保它们包含文本。如果没有,它将显示一个包含精心格式自定义消息的 UIAlertView。
安装
Carthage 安装
Carthage 是一个去中心化的依赖项管理器,构建您的依赖项并提供二进制框架。要将 TextFieldValidator 集成到您的 Xcode 项目中并使用 Carthage,请在您的 Cartfile 中指定它
github "froggomad/TextFieldValidator"
然后将生成的框架(通常位于 Carthage/Build
文件夹中)添加到项目中,就像平常一样。
Cocoapods 安装
CocoaPods 是用于 Cocoa 项目的依赖管理器。有关使用和安装说明,请访问他们的 网站。要使用 CocoaPods 将 TextFieldValidator 集成到您的 Xcode 项目中,请指定 Podfile
pod 'TextFieldValidator'
然后从您的项目目录在命令行中运行
pod install
示例用法
@IBOutlet private var nameField: UITextField!
@IBOutlet private var emailField: UITextField!
@IBAction func doneButtonTapped(_ sender: UIButton) {
let validator = TextFieldValidator(viewController: self)
//set messages for specific UITextFields via subscripting:
validator[emailField] = "E-mail Address"
//or use the TextFieldValidator.set method:
validator.set(
nameField,
with: "Name"
)
//check all textFields that have been set on this validator and alert a message for each empty textField:
validator.alertIfEmpty(title: "Please Enter Your:")
//for more complex operations, there's a closure that returns Void
/*
validator.alertIfEmpty() {
//The getText method returns the textField's text safely unwrapped or an empty String if it fails
print(self.validator.getText(from: self.nameField))
}
*/
}