2009-10-20 36 views
0
- (void)cancel { 
// [managedObjectContext.undoManager disableUndoRegistration]; 
    [managedObjectContext deleteObject:object]; // I don't want this deletion to be recorded on the undo stack which is empty at this point. 
// [managedObjectContext.undoManager enableUndoRegistration]; 
    [managedObjectContext.undoManager removeAllActions]; 
} 

使用此代碼,我仍然在撤消堆棧上進行了刪除,即使取消註釋這兩行也無法阻止錄製。爲什麼?爲什麼此代碼未能清空撤消堆棧

回答

2

你需要禁用之前和之前重新啓用撤消處理,呼籲管理對象上下文-processPendingChanges。核心數據延遲註冊撤消事件,直到processPendingChanges,以允許將多個更改合併到相同的屬性和類似的快捷方式。

0

什麼叫-cancel?我遇到了其他方法註冊撤消事件的情況(核心數據可能會「幫助」爲您做這件事)。您可能會發現撤銷堆棧在-cancel結尾處爲空,但獲取調用方法添加的事件。

在這種情況下,選擇「取消」,實際上並不(UN)做任何事情,事件無關撤消。爲了防止這些多餘的事件,您需要在調用方法中禁用撤銷註冊(然後重新啓用它)。沿着線的東西...

- (void)doSomething { 
    // Disable undo until we definitely want to undo stuff 
    [managedObjectContext.undoManager disableUndoRegistration]; 

    // Stuff goes here 

    if (userCancelled) { 
     [self cancel]; 
     [managedObjectContext.undoManager enableUndoRegistration]; 
     return; 
    } 

    // We actually want user to be able to undo the following stuff 
    [managedObjectContext.undoManager enableUndoRegistration]; 

    // More stuff goes here 
} 

鑽研太遠此之前做一些簡單的NSLog檢查,看看是什麼在撤消堆棧上時(並確保你將消息發送到真正的活NSUndoManager而比無零件)。

+0

1.一個按鈕觸發 - 取消。 2.我怎麼才能找到撤消堆棧是否爲空,除了試圖做撤銷?這個信息被封裝,所以我不能登錄。 3.我的「撤消」確實觸發了一個撤消我的刪除操作。 4.無撤銷管理器?哦,不,當然。但感謝提醒。 – an0