Skip to content

Instantly share code, notes, and snippets.

@HassanElDesouky
Created August 28, 2019 07:19
Show Gist options
  • Save HassanElDesouky/8de1de200c0f3b99313ecefab4d7666b to your computer and use it in GitHub Desktop.
Save HassanElDesouky/8de1de200c0f3b99313ecefab4d7666b to your computer and use it in GitHub Desktop.
class CreateListController: UITableViewController {
@IBOutlet weak var doneBarButton: UIBarButtonItem!
@IBOutlet weak var nameTextField: UITextField!
// ..
// ..
var chooseIconTapped = false
override func viewDidLoad() {
super.viewDidLoad()
handleEmptyFields()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
checkFields()
}
func handleEmptyFields() {
doneBarButton.isEnabled = false
nameTextField.delegate = self
}
func checkFields() {
if list != nil || chooseIconTapped && nameTextField.text!.count > 0 {
doneBarButton.isEnabled = true
}
}
}
extension CreateListController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = (nameTextField.text! as NSString).replacingCharacters(in: range, with: string)
if text.isEmpty || !chooseIconTapped {
doneBarButton.isEnabled = false
} else {
doneBarButton.isEnabled = true
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment