Chapter 16 - 'Done' Button never active

So I just completed Chapter 16 and when i run the simulator the ‘Done’ button on the add a checklist is never active. I’ve cross compared my code with the final code and I’ve checked my connections and it is indeed connected to the reference outlet and the sent action. I can use return on the keyboard and that will work but when I type the done button doe not become active. Any ideas what else I can check?

Edit: You posted this within the “Swift Apprentice” book forum, your issue is in chapter 16 of the “iOS Apprentice” book :wink: → iOS Apprentice Forums

If the Done Bar Button is still inactive after you’ve typed text into textField it could be your code involving @IBOutlet weak var doneBarButton: UIBarButtonItem!

First, make sure @IBOutlet weak var doneBarButton is wired to the Done Bar Button in storyboard

Specific Code to look out for

ListDetailViewController :: UI Connections
  // MARK: - UI Connections
  
  @IBOutlet weak var textField: UITextField!
  @IBOutlet weak var doneBarButton: UIBarButtonItem! // Make sure it's wired!
  
  @IBAction func cancel() {
    delegate?.listDetailViewControllerDidCancel(self)
  }
  
  @IBAction func done() {
    if let checklist = checklistToEdit {             // if !nil, edit
      checklist.name = textField.text!
      delegate?.listDetailViewController(self,       // editCurrent
                                         didFinishEditing: checklist)
    } else {                                         // nil, addNew
      let checklist = Checklist(name: textField.text!)
      delegate?.listDetailViewController(self,
                                         didFinishAdding: checklist)
    }
  }

ListDetailViewController :: viewDidLoad() & viewWillAppear()
  // MARK: - ViewController Life Cycle
  
  /// Called after the controller'€™s view is loaded into memory.
  override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.largeTitleDisplayMode = .never
    
    if let checklistToEdit = checklistToEdit {  // if !nil, edit
      title = "Edit Checklist"                  // Set Title on NavItem
      textField.text = checklistToEdit.name
      doneBarButton.isEnabled = true            // Make sure this is TRUE
    }
  }
  
  /// Notifies the view controller that its view is about to be added to a view hierarchy.
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    textField.becomeFirstResponder()
  }

ListDetailViewController :: textField(_:shouldChangeCharactersIn:replacementString:)
// MARK: - UITextField Delegate

extension ListDetailViewController: UITextFieldDelegate {
  
  /// Asks the delegate if the specified text should be changed.
  func textField(_ textField: UITextField,
                 shouldChangeCharactersIn range: NSRange,
                       replacementString string: String) -> Bool {
    let oldText = textField.text!
    let stringRange = Range(range,
                            in: oldText)!
    let newText = oldText.replacingCharacters(in: stringRange,
                                            with: string)
    doneBarButton.isEnabled = !newText.isEmpty  // Make sure this is in your code!
    return true
  }
}


If it still doesn’t work…

Storyboard/InterfaceBuilder

Select the Done Bar Button
make sure Enabled is unchecked in it’s Attributes Inspector
image

It's Connections Inspector looks like:

image

Check the Connection Inspector

Select the Add Checklist ViewController Object

image

It's Connection Inspector should look like this:

Hope this helps!
Rebecca - i2j3

Sorry, I didn’t realize I put it into the wrong forum :man_facepalming:t3:

I ended up figuring it out, it was actually none of the above lol I had the text field delegate connected to ‘Table View’ and not to ‘Add Checklist’, once I switched that up it work!!

Thanks for the help though!

This topic was automatically closed after 166 days. New replies are no longer allowed.