Last active
April 8, 2024 23:24
-
-
Save kouki-dan/1e08c913fae321ea0c5dd0bd26798502 to your computer and use it in GitHub Desktop.
Swift UndoManager usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class Counter { | |
let manager = UndoManager() | |
var count = 0 | |
@objc func increment() { | |
count += 1 | |
manager.registerUndo(withTarget: self, selector: #selector(Counter.decrement), object: nil) | |
} | |
@objc func decrement() { | |
count -= 1 | |
manager.registerUndo(withTarget: self, selector: #selector(Counter.increment), object: nil) | |
} | |
} | |
let counter = Counter() | |
print("Let's increment!") | |
counter.increment() | |
print("count: \(counter.count)") | |
print("Let's undo!") | |
counter.manager.undo() | |
print("count: \(counter.count)") | |
print("Let's redo!") | |
counter.manager.redo() | |
print("count: \(counter.count)") // It returns 1 :) | |
/** Outputs | |
Let's increment! | |
count: 1 | |
Let's undo! | |
count: 0 | |
Let's redo! | |
count: 1 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class Counter { | |
var count = 0 | |
} | |
let counter = Counter() | |
let manager = UndoManager() | |
print("Let's increment!") | |
counter.count += 1 | |
manager.registerUndo(withTarget: counter, handler: { counter in | |
counter.count -= 1 | |
print("Decremented by undo") | |
}) | |
print("count: \(counter.count)") | |
print("Let's undo!") | |
manager.undo() | |
print("count: \(counter.count)") | |
print("Let's redo!") | |
manager.redo() | |
print("count: \(counter.count)") // It should be 1, but returns 0. Why? | |
/** Outputs | |
Let's increment! | |
count: 1 | |
Let's undo! | |
Decremented by undo | |
count: 0 | |
Let's redo! | |
count: 0 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class Counter { | |
var count = 0 | |
} | |
let counter = Counter() | |
let manager = UndoManager() | |
print("Let's increment!") | |
counter.count += 1 | |
manager.registerUndo(withTarget: counter, handler: { counter in | |
counter.count -= 1 | |
print("Decremented by undo") | |
// It needs registering for redo | |
manager.registerUndo(withTarget: counter, handler: { counter in | |
counter.count += 1 | |
}) | |
}) | |
print("count: \(counter.count)") | |
print("Let's undo!") | |
manager.undo() | |
print("count: \(counter.count)") | |
print("Let's redo!") | |
manager.redo() | |
print("count: \(counter.count)") // It returns 1 :) | |
/** Outputs | |
Let's increment! | |
count: 1 | |
Let's undo! | |
Decremented by undo | |
count: 0 | |
Let's redo! | |
count: 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment