Skip to content

Instantly share code, notes, and snippets.

@oaleeapp
Created May 6, 2019 02:07
Show Gist options
  • Save oaleeapp/f01d346406d282ac0fc1848c57bf8470 to your computer and use it in GitHub Desktop.
Save oaleeapp/f01d346406d282ac0fc1848c57bf8470 to your computer and use it in GitHub Desktop.
Set up TextFields
class ViewController {
@IBOutlet weak var emailTextField: OyaRoundCornerTextField!
@IBOutlet weak var passwordTextField: OyaRoundCornerTextField!
// ...
var textFields: [UITextField] {
get {
return [emailTextField,passwordTextField]
}
}
/// ...
}
extension ViewController: UITextFieldDelegate {
func setUpTextFields() {
for textField in textFields {
textField.delegate = self
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
guard let index = textFields.index(of: textField) else {
return true
}
if index + 1 < textFields.count {
let nextField = textFields[index + 1]
nextField.becomeFirstResponder()
} else {
textField.resignFirstResponder()
}
return false
}
@objc func textChanged(textField: UITextField) {
textField.text = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines)
}
func resignTextFieldFirstResponder() {
for textField in textFields {
textField.resignFirstResponder()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment