Created
May 6, 2019 02:07
-
-
Save oaleeapp/f01d346406d282ac0fc1848c57bf8470 to your computer and use it in GitHub Desktop.
Set up TextFields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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