简体   繁体   中英

textDidChange points to the wrong NSCollectionViewItem

I am trying to build an NSCollectionView filled with multiple editable TextViews. (OS X app in Swift.) My subclass of NSCollectionViewItem is called NoteViewItem. I am trying to have the program detect when one of the TextView has changed. I tried using both controlTextDidChange and textDidChange in the NoteViewItem's delegate with test print statement to see which would work. ControlTextDidChange did nothing; textDidChange recognized a change happened, so I went with that.

The problem is that textDidChange appears to point to a different NoteViewItem than the one that was shown on screen in the first place. It wasn't able to recognize the variable (called theNote) set in the original NoteViewItem; when I ask NoteViewItem to print String(self), I get two different results, one while setting the initial text and one in textDidChange. I'm wondering if I've set up my delegates and outlets wrongly. Any thoughts on why my references are off here?

Here's my code for NoteViewItem:

import Cocoa

class NoteViewItem: NSCollectionViewItem, NSTextViewDelegate
{        
    // MARK: Variables

    @IBOutlet weak var theLabel: NSTextField!
    @IBOutlet var theTextView: NSTextView!

    var theNote: Note?
    {
        didSet
        {
            // Pre: The NoteViewItem's theNote property is set.
            // Post: This observer has set the content of the *item's text view*, and label if it has one.

            guard viewLoaded else { return }

            if let theNote = theNote
            {
                // textField?.stringValue = theNote.noteText
                theLabel.stringValue = theNote.filename
                theTextView.string = theNote.noteText
                theTextView.display()

                print("theTextView.string set to "+theTextView.string!+" in NoteViewItem "+String(self))
            }
            else
            {
                theLabel.stringValue = "Empty note?"
            }
        }
    }


    // MARK: Functions

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.

        // Hopefully this will set the note's background to white. 
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.whiteColor().CGColor

    }

    // MARK: - NSTextViewDelegate

    /*
    override func controlTextDidChange(notification: NSNotification)
    {
        print("Control text changed.")
    }
    */


    func textDidChange(notification: NSNotification)
    {            
        if let noteyMcNoteface = theNote
        {
            print("On edit, we have a note: "+String(noteyMcNoteface))
        }
        else
        {
            print("On edit, we have no note. I am NoteViewItem "+String(self))
        }   
    }   
}

I figured it out. My delegate, in the TextView, was connected to the wrong object in the Interface Builder for NoteViewItem.xib. I had connected it to the object labelled Note View Item, under objects in the outline. It should have been connected to File's Owner instead, since File's Owner stands for the NoteViewItem.swift class associated with the xib.

You'd think that if you want to connect the delegate to the NoteViewItem class and there is exactly one Note View Item listed in the outline, then that Note View Item is the thing you want to connect it to. Nope, you connect it to something entirely different that isn't called the Note View Item but is the Note View Item. I'm glad Interface Builder makes things so simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM