title: Implementing redo alongside undo with UndoManager in drawing application Swift 3
body: <p>I am working on a project that includes an annotation tool allowing users to "draw" on documents with finger gestures or a pencil. Naturally, I'm keen on implementing undo/redo for drawn paths.</p>
  
  <p>My implementation for the drawing app is relatively conventional. What the user sees on the screen is the combination of a cached bitmap image (a snapshot of all the paths that have been drawn before the current one) together with a "live" rendering of the current path (a UIBezierPath). When touchesEnded is triggered, the new path is added to the bitmap.</p>
  
  <p>I have been able to implement undo with relatively little trouble. I have created a standard undoManager for the class:</p>
  
  <pre><code>let myUndoManager : UndoManager = {
      let mUM : UndoManager = UndoManager()
      mUM.levelsOfUndo = 6
      return mUM
  }()
  </code></pre>
  
  <p>The function that is called at the end of touchesEnded to render the new cached path is called drawBitmap. At the start of this function, assuming there is a previous cached path and before drawing the new one, I register the following undo action with the undo manager:</p>
  
  <pre><code>let previousCachedPath : UIImage = self.cachedPath
  self.myUndoManager.registerUndo(withTarget: self, selector: #selector(self.setBitmap(_:)), object: previousCachedPath)
  </code></pre>
  
  <p>setBitmap(_ previousCachedPath : UIImage) is a function that resets the displayed bitmap to the provided image. </p>
  
  <p>I have a undo/redo buttons linked to undo() and redo() methods respectively. Aside from some logic dictating when these buttons should be active (i.e. to make sure you can't press undo when nothing has been drawn etc.), these simply call myUndoManager.undo() and myUndoManager.redo() respectively:</p>
  
  <pre><code>func undo() -&gt; Void {
      guard self.myUndoManager.canUndo else { return }
      self.myUndoManager.undo()
      if !self.redoButton.isEnabled {
          self.redoButton.isEnabled = true
      }
      if !self.myUndoManager.canUndo {
          self.undoButton.isEnabled = false
      }
  
      self.setNeedsDisplay()
  }
  
  func redo() -&gt; Void {
      guard self.myUndoManager.canRedo else { return }
      self.myUndoManager.redo()
      if !self.undoButton.isEnabled {
          self.undoButton.isEnabled = true
      }
      if !self.myUndoManager.canRedo {
          self.redoButton.isEnabled = false
      }
  
      self.setNeedsDisplay()
  }
  </code></pre>
  
  <p>As I mentioned, undo works perfectly to the specified six levels of undoability. However, I'm clearly missing something with redo. My initial hope was that the undoManager would automatically transfer undo tasks from the undo stack to the redo stack when undo is called, but this is clearly not happening.</p>
  
  <p>I have already searched for answers, and I think the closest to what I need may to use mutual recursion as per:</p>
  
  <p><a href="https://stackoverflow.com/questions/36491789/using-nsundomanager-how-to-register-undos-using-swift-closures">Using NSUndoManager, how to register undos using Swift closures</a></p>
  
  <p>However, I have been unable to make this work. Any help therefore appreciated!</p>