Mat*_*all 8 cocoa core-data undo objective-c
在我的核心数据模型中,我有一个关系,称为listItems链接到几个listItem实体,每个实体都有一个stringValue属性.我创建了一个控件,它实际上是一个列表NSTextFields,每个列表项都有一个.控件绑定listItems正确,我已将其设置为按下返回键直接在当前编辑的字段下创建一个新字段,并将焦点更改为新字段.因此,基本上,要添加新项目,用户按下Return.
同样,如果用户结束编辑并且当前编辑的字段为空,则删除该字段(如图所示,空字段仅在"编辑模式"期间出现,可以这么说).这非常有效.基本上,在我的listItemNSManagedObject子类中,我执行以下操作:
// Don't allow nil values
if (!value && [[self.recipe ingredients] count] > 1) {
for (EAIngredientRef *ingredient in [self.recipe ingredients]) {
if ([[ingredient sortIndex] integerValue] > [[self sortIndex] integerValue]) {
[ingredient setSortIndex:[NSNumber numberWithInteger:([[ingredient sortIndex] integerValue]-1)]];
}
}
[[self managedObjectContext] deleteObject:self];
return;
}
// Code to handle if it is a real value
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,每次以这种方式删除行时,它都会注册undoManager.因此,如果我编辑一行,请按Return键(创建一个新行),然后单击"离开"以结束编辑,该行将消失.但是,如果我然后撤消,则会再次出现空字段.我的目标是让undoManager忽略涉及空字段的删除操作.
我该怎么做?我已尝试在几个地方使用[[[self managedObjectContext] undoManager] disableUndoRegistration]和关联enableUndoRegistration(例如-didTurnIntoFault,但我怀疑撤销注册可能在该方法之前发生)
Mik*_*lah 19
如果您更深入地深入了解核心数据文档,您会发现隐藏的这个花絮:
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// Do your work
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
Run Code Online (Sandbox Code Playgroud)
在事件循环结束之前,通常不会在撤消管理器中注册更改,因此在您重新启用撤消注册后进行注册.以上迫使它在你想要时发生.