This version of TextFieldValidator validates UITextFields to ensure that they contain text. If not, it presents a UIAlert with a nicely formatted custom message.
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate TextFieldValidator into your Xcode project using Carthage, specify it in your Cartfile:
github "froggomad/TextFieldValidator"
Then add the resulting Framework (normally found in the Carthage/Build
folder) to your project as you normally would.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate TextFieldValidator into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'TextFieldValidator'
Then from your project's directory in the command line, run:
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))
}
*/
}