Created
August 28, 2019 07:19
-
-
Save HassanElDesouky/8de1de200c0f3b99313ecefab4d7666b to your computer and use it in GitHub Desktop.
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
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