Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
Created March 18, 2021 05:58
Show Gist options
  • Select an option

  • Save DavidPiper94/485eed68df50b0a8a0d44eb7f37ceca0 to your computer and use it in GitHub Desktop.

Select an option

Save DavidPiper94/485eed68df50b0a8a0d44eb7f37ceca0 to your computer and use it in GitHub Desktop.
Example code for article about UndoManager - Recording Commands
// 1
func redButtonPressed(_ sender: UIButton) {
recordColorChangeCommand(newColor: .red)
}
// 2
func recordColorChangeCommand(newColor: UIColor) {
let currentColor = colorView.backgroundColor
let command = Command(
execute: { self.colorView.backgroundColor = newColor },
reverse: { self.colorView.backgroundColor = currentColor }
)
historyService.record(command: command)
updateActionButtons()
}
// 3
func updateActionButtons() {
undoButton.isEnabled = historyService.canUndo
redoButton.isEnabled = historyService.canRedo
}
// 4
func undoButtonPressed(_ sender: UIButton) {
historyService.undo()
updateActionButtons()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment