2011-04-17 56 views
2

在我的應用程序中,有一些操作我想以編程方式撤銷,而不給用戶單擊「重做」的選項。有沒有辦法清除重做堆棧NSUndoManager?如果沒有,並且我要繼承NSUndoManager,有什麼方法可以訪問重做堆棧以清除它?我從文檔中找不到任何方法。清除NSUndoManager的重做堆棧

或者,有沒有辦法在沒有填充重做堆棧的情況下恢復當前嵌套撤消組的更改?我已經構建了一個嵌套的撤消組。

回答

1

我最終採取了兩步法。第一步是創建一個虛擬的撤消項目,清除重做堆棧。然後,我只需要移除這個撤消項目,並且這兩個堆棧都是乾淨的。

我能夠使用self作爲僞撤消目標,因爲我沒有任何與包含代碼的類關聯的實際撤消操作。 self可以替換爲任何對Undo堆棧沒有貢獻的對象。

訣竅是延遲呼叫removeAllActionsWithTarget,否則它不起作用。

// End the open undo grouping 
[undoManager endUndoGrouping]; 

// Perform the undo operation, which gets pushed onto the Redo stack 
[undoManager undo]; 

// Add a dummy Undo item to clear the Redo stack 
[undoManager registerUndoWithTarget:self selector:nil object:nil]; 

// Remove the dummy item with a delay, pushing it to the next run loop cycle 
[undoManager performSelector:@selector(removeAllActionsWithTarget:) 
        withObject:self 
        afterDelay:0.0]; 
1
[undoManager disableUndoRegistration]; 
[undoManager undo]; 
[undoManager enableUndoRegistration]; 
+0

這造成了一個例外:「撤消叫了太多的嵌套撤消羣體」,我是否不關閉只開放撤銷事先分組。這是否與所有這些調用都處於運行循環的相同循環中有關? – Dov 2011-04-18 19:42:01