Skip to content

Instantly share code, notes, and snippets.

@aniltv06
Created March 30, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniltv06/85aee5ead95e50dce9caf82c08651714 to your computer and use it in GitHub Desktop.
Save aniltv06/85aee5ead95e50dce9caf82c08651714 to your computer and use it in GitHub Desktop.
UIAlertController with text field and disable buttons if textfield is empty
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