從這個問題和你約NSArrayControllers相關的問題,我收集你正在做這樣的事情:
- (void)makeWindowControllers
{
MyWindowController* wc = [[[MyWindowController alloc] initWithWindowNibName: [self windowNibName]] autorelease];
[self addWindowController: wc];
}
當你做到這一點,-windowControllerDidLoadNib:
將不會被調用,因爲NSDocument對象如果你是這樣開始的話,那麼它不是Nib的所有者。如果你看一下NSDocument.h
你會看到這樣的評論(見強調):
/* Create the user interface for this document, but don't show it yet. The
default implementation of this method invokes [self windowNibName],
creates a new window controller using the resulting nib name (if it is
not nil), **specifying this document as the nib file's owner**, and then
invokes [self addWindowController:theNewWindowController] to attach it.
You can override this method to use a custom subclass of
NSWindowController or to create more than one window controller right
away. NSDocumentController invokes this method when creating or opening
new documents.
*/
- (void)makeWindowControllers;
如果是你,而不是,這樣做:
- (void)makeWindowControllers
{
MyWindowController* wc = [[[MyWindowController alloc] initWithWindowNibName: [self windowNibName] owner: self] autorelease];
[self addWindowController: wc];
}
我相信你會發現-windowControllerDidLoadNib:
被稱爲再次。這可能幫不了你,如果你有不是NSDocument一個很好的理由筆尖的所有者,但是這就是爲什麼-windowControllerDidLoadNib:
不會被調用,什麼可以做,以獲得該行爲了。這幾乎可以肯定是一個比init更好的提取位置,這可能發生在所有必要的CoreData支持工具到位之前。所以這是一種選擇。
謝謝,這就是我尋找的東西,你應該清醒的唯一的事情是你無法保存上下文。無論如何,這是由NSDocument/NSPersistentDocument覆蓋。 – Stephan
你是什麼意思 - 你不能在-makeWindowControllers中保存上下文? – paulmelnikow
不要在'NSManagedObjectContext'實例上調用'save'' – Stephan