Created
March 30, 2018 06:41
-
-
Save aniltv06/85aee5ead95e50dce9caf82c08651714 to your computer and use it in GitHub Desktop.
UIAlertController with text field and disable buttons if textfield is empty
This file contains 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
let alertController = UIAlertController(title: nil, message: alertMessageString, preferredStyle: .alert) | |
let cancelAction = UIAlertAction(title: alertLeftBtuTitle, style: .default, handler: nil) | |
alertController.addAction(cancelAction) | |
let okAction = UIAlertAction(title: alertRightBtnText , style: UIAlertActionStyle.default, handler: { | |
(action) -> Void in | |
print("ok tapped") | |
}) | |
okAction.isEnabled = false | |
alertController.addAction(okAction) | |
alertController.addTextField(configurationHandler: {(textField: UITextField!) in | |
textField.placeholder = "Reject reason:" | |
textField.isSecureTextEntry = true | |
NotificationCenter.default.addObserver(forName: .UITextFieldTextDidChange, object: textField, queue: OperationQueue.main, using: | |
{_ in | |
let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0 | |
let textIsNotEmpty = textCount > 0 | |
// If the text contains non whitespace characters, enable the OK Button | |
okAction.isEnabled = textIsNotEmpty | |
}) | |
} | |
DispatchQueue.main.async { | |
self.present(alertController, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment