Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static func createLoginDialog(buttonPressed: @escaping (_ username:String, _ pw:String) -> Void) -> UIAlertController {
- let alert = UIAlertController(title: "Login", message: "", preferredStyle: .alert)
- let loginAction = UIAlertAction(title: "Log in", style: .default, handler: { _ in
- let u = alert.textFields![0].text!
- let p = alert.textFields![1].text!
- buttonPressed(u,p)
- })
- alert.addAction(loginAction)
- loginAction.isEnabled = false
- var textFields: [UITextField] = []
- alert.addTextField { textField in
- textField.placeholder = "Enter Username"
- textFields.append(textField)
- NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification,
- object: textField,
- queue: .main) { _ in
- if textFieldsAreValid(textFields) {
- loginAction.isEnabled = true
- }
- }
- }
- alert.addTextField { textField in
- textField.placeholder = "Enter Password"
- textFields.append(textField)
- NotificationCenter.default.addObserver(forName: UITextField.textDidChangeNotification,
- object: textField,
- queue: .main) { _ in
- if textFieldsAreValid(textFields) {
- loginAction.isEnabled = true
- }
- }
- }
- func textFieldsAreValid(_ textFields: [UITextField]) -> Bool {
- textFields.allSatisfy { !($0.text ?? "").isEmpty } //Do the needed tests, I just check if not empty
- }
- return alert
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement