gpt4 book ai didi

swift - textDidChange 指向错误的 NSCollectionViewItem

转载 作者:行者123 更新时间:2023-11-30 13:15:38 24 4
gpt4 key购买 nike

我正在尝试构建一个充满多个可编辑 TextView 的 NSCollectionView。 (Swift 中的 OS X 应用程序。)我的 NSCollectionViewItem 子类称为 NoteViewItem。我试图让程序检测 TextView 之一何时发生更改。我尝试在 NoteViewItem 的委托(delegate)中使用 controlTextDidChange 和 textDidChange 以及测试打印语句来查看哪个有效。 ControlTextDidChange 什么也没做; textDidChange 认识到发生了变化,所以我接受了。

问题是 textDidChange 似乎指向一个与屏幕上首先显示的 NoteViewItem 不同的 NoteViewItem。它无法识别原始 NoteViewItem 中设置的变量(称为 theNote);当我要求 NoteViewItem 打印 String(self) 时,我得到两种不同的结果,一种是在设置初始文本时得到的,另一种是在 textDidChange 中得到的。我想知道我的代表和 channel 是否设置错误。对于为什么我的引用资料不在此处有什么想法吗?

这是我的 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))
}
}
}

最佳答案

我明白了。我的委托(delegate)在 TextView 中连接到 Interface Builder 中 NoteViewItem.xib 的错误对象。我已将其连接到大纲中对象下标记为“注释 View 项目”的对象。它应该连接到 File's Owner,因为 File's Owner 代表与 xib 关联的 NoteViewItem.swift 类。

认为如果您想要将委托(delegate)连接到 NoteViewItem 类,并且大纲中恰好列出了一个 Note View Item,那么该 Note View Item 就是您想要的将其连接到。不,您将它连接到完全不同的东西,不称为注释 View 项,而是注释 View 项。我很高兴 Interface Builder 让事情变得如此简单。

关于swift - textDidChange 指向错误的 NSCollectionViewItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com