title: Enable a button in Swift only if all text fields have been filled out
body: <p>I am having trouble figuring out how to change my code to make it so the Done button in the navigation bar is enabled when my three text fields are filled out.</p>
  
  <p>I currently have three UITextFields and one UIButtonItem. Both the habitNameField and the goalField are manual text fields, and the frequencyField is a Picker View.</p>
  
  <pre><code>@IBOutlet weak var habitNameField: UITextField!
  @IBOutlet weak var goalField: UITextField!
  @IBOutlet weak var frequencyField: UITextField!
  
  @IBOutlet weak var doneBarButton: UIBarButtonItem!
  </code></pre>
  
  <p>I also have the following function that works when there is something typed in the first field.</p>
  
  <pre><code>func textField(habitNameField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
      let oldHabitNameText: NSString = habitNameField.text!
      let newHabitNameText: NSString = oldHabitNameText.stringByReplacingCharactersInRange(range, withString: string)
      doneBarButton.enabled = (newHabitNameText.length != 0)
      return true
  }
  </code></pre>
  
  <p>I tried change the code so that it took in the other two fields as parameters and enabled the doneBarButton only if all three fields were filled out. </p>
  
  <pre><code>func textField(habitNameField: UITextField, goalField: UITextField, frequencyField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
      let habitNameText: NSString = (habitNameField.text!).stringByReplacingCharactersInRange(range, withString: string)
      let goalText: NSString = (goalField.text!).stringByReplacingCharactersInRange(range, withString: string)
      let frequencyText: NSString = (frequencyField.text!).stringByReplacingCharactersInRange(range, withString: string)
  
      doneBarButton.enabled = (habitNameText.length != 0) &amp;&amp; (goalText.length != 0) &amp;&amp; (frequencyText.length != 0)
      return true
  }
  </code></pre>
  
  <p>However, it's not working, even when I fill out all three text fields.</p>
  
  <p>I would really appreciate any help, and thanks to anyone who contributes in advance!</p>
  
  <p>All code here:</p>
  
  <pre><code>class HabitDetailViewController: UITableViewController, UITextFieldDelegate, UIPickerViewDataSource,UIPickerViewDelegate {
  @IBOutlet weak var habitNameField: UITextField!
  @IBOutlet weak var goalField: UITextField!
  @IBOutlet weak var doneBarButton: UIBarButtonItem!
  @IBOutlet weak var frequencyField: UITextField!
  
  var frequencies = ["Day", "Week", "Month", "Year"]
  var frequencyPicker = UIPickerView()
  
  var habitToEdit: HabitItem?
  weak var delegate: HabitDetailViewControllerDelegate?
  
  @IBAction func cancel() {
      delegate?.habitDetailViewControllerDidCancel(self)
  }
  
  @IBAction func done() {
      print("You plan to do \(habitNameField.text!) \(goalField.text!) times a \(frequencyField.text!.lowercaseString).")
      if let habit = habitToEdit {
          habit.name = habitNameField.text!
          habit.numberLeft = Int(goalField.text!)!
          habit.frequency = frequencyField.text!
          delegate?.habitDetailViewController(self, didFinishEditingHabit: habit)
      } else {
          let habit = HabitItem()
          habit.name = habitNameField.text!
          habit.numberLeft = Int(goalField.text!)!
          habit.frequency = frequencyField.text!
          habit.completed = false
          delegate?.habitDetailViewController(self, didFinishAddingHabit: habit)
      }
  }
  
  override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
      habitNameField.becomeFirstResponder()
      frequencyPicker.hidden = false
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
      frequencyPicker.dataSource = self
      frequencyPicker.delegate = self
      doneBarButton.enabled = false
      habitNameField.addTarget(self, action: "checkFields:", forControlEvents: .EditingChanged)
      goalField.addTarget(self, action: "checkFields:", forControlEvents: .EditingChanged)
      frequencyField.addTarget(self, action: "checkFields:", forControlEvents: .EditingChanged)
      frequencyField.inputView = frequencyPicker
      if let habit = habitToEdit {
          title = "Edit Item"
          habitNameField.text = habit.name
          goalField.text = String(habit.numberLeft)
          doneBarButton.enabled = true
      }
  }
  
  override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -&gt; NSIndexPath? {
      return nil
  }
  
  func textField(habitNameField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -&gt; Bool {
      let oldHabitNameText: NSString = habitNameField.text!
      let newHabitNameText: NSString = oldHabitNameText.stringByReplacingCharactersInRange(range, withString: string)
      doneBarButton.enabled = (newHabitNameText.length != 0)
      return true
  }
  
  func checkFields(sender: UITextField) {
      sender.text = sender.text?.stringByTrimmingCharactersInSet(.whitespaceCharacterSet())
      guard
          let habit = habitNameField.text where !habit.isEmpty,
          let goal = goalField.text where !goal.isEmpty,
          let frequency = frequencyField.text where !frequency.isEmpty
          else { return }
      // enable your button if all conditions are met
      doneBarButton.enabled = true
  }
  </code></pre>