Checklists: 14 Editing Items

Hit my first roadblock.

I thought I was following everything step by step. I am getting no errors, but when I click the i icon to Edit an item, the Checklist Item to be edited does not load in the text field. It is always blank.

override func viewDidLoad() {
    super.viewDidLoad()
    if let item = itemToEdit {
        title = "Edit Item"
        textField.text = item.text
        doneBarButton.isEnabled = true
    }
}

I know the code block is running because the title is changing to Edit Item and the doneBarButton is enabled when I type text. I used File Merge and could not find any significant differences. The code block below is the only thing responsible for loading the something in the textfield right?

I have cleaned the Build Folder, Deleted Derived Data Folder, Restarted Xcode and still nothing.

Have you tried setting a breakpoint on the line where the item text is loaded, or printing the item text to the console at that point to see if the item has any text to be loaded?

If everything is working correctly, then my guess would be that either item.text is empty or that something else is overwriting the value in the text field. But without seeing what you’re seeing, it’s hard to know what is going on.

If you still can’t figure it out, please upload your project as a ZIP file somewhere and provide a link to the ZIP file so that I (or someone else) can download the file and see what might be going on.

Just checked with print statement and it works. Not sure how a breakpoint would help.

It says new users cannot upload items. So yeah…

My ItemDetailViewController

import UIKit

protocol ItemDetailViewControllerDelegate: class {
    func itemDetailViewControllerDidCancel(_ controller: ItemDetailViewController)
    func itemDetailViewController(_ controller: ItemDetailViewController, didFinishAdding item: ChecklistItem)
    func itemDetailViewController(_ controller: ItemDetailViewController, didFinishEditing item: ChecklistItem)
}

class ItemDetailViewController: UITableViewController, UITextFieldDelegate {
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var doneBarButton: UIBarButtonItem!
    
    weak var delegate: ItemDetailViewControllerDelegate?
    var itemToEdit: ChecklistItem?

    override func viewDidLoad() {
        super.viewDidLoad()
        if let item = itemToEdit {
            title = "Edit Item"
            textField.text = item.text
            doneBarButton.isEnabled = true
        }
    }
    
    // MARK:- Text Field Focus
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        textField.becomeFirstResponder()
    }

    // MARK:- Actions
    @IBAction func done() {
        if let item = itemToEdit {
            item.text = textField.text!
            delegate?.itemDetailViewController(self, didFinishEditing: item)
        } else {
            let item = ChecklistItem()
            item.text = textField.text!
            delegate?.itemDetailViewController(self, didFinishAdding: item)
        }
    }
    
    @IBAction func cancel(_ sender: UIBarButtonItem) {
        delegate?.itemDetailViewControllerDidCancel(self)
    }
    
    // MARK:- Table View Delegates
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return nil
    }
    
    // MARK:- Text Field Delegates
    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
        
        return true
    }
    
    // Disables Done Button when clear is enabled,
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        doneBarButton.isEnabled = false
        return true
    }
    
}

https://drive.google.com/open?id=1fzUYtyk9j65R06JZKoGDiT2am0S8gDWV

Ok hopefully this works.

The zip works, except it is missing the enclosing “Checklists” folder, but that can be fixed up.

After some tracing and head scratching, I figured out that in the storyboard, the Text Field has the “Clear when editing begins” checkbox checked. As a result, it clears the text when editing begins. :slight_smile:

2 Likes

Facepalm :man_facepalming:t2:

Thanks so much!

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