How to enable undo menu item when a sheet is presented

457 Views Asked by At

I am creating a document based core data OSX app using storyboards. Undo and redo works fine until I present a View Controller in a sheet with a segue. Once the sheet is presented, the undo / redo buttons are grayed out.

While searching for a possible solution, I came across this article in which they say that I have to supply an undo manager to my window using the "windowWillReturnUndoManager:" delegate method. So I implemented this method in the sourceController of my segue, and set that controller as the delegate for the window of the destinationController in the prepareForSegue method like this:

override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
    super.prepareForSegue(segue, sender: sender)
    (segue.destinationController as NSViewController).view.window?.delegate = self
}

func windowWillReturnUndoManager(window: NSWindow) -> NSUndoManager? {
    println(undoManager)
    return undoManager
}

But the undo and redo buttons are still grayed out when I open the sheet. Note that when change the segue style to popover, the undo/redo are working perfectly. How can I resolve this?

1

There are 1 best solutions below

0
On

I had the same problem and am posting here in case it might help someone. My solution was to obtain the undo manager for the window to which the sheet view controller is attached:

let undoManager = self.view.window?.firstResponder?.undoManager

self in this context is the view controller for the sheet and not the parent view controller for the sheet. Therefore, the assignment would take place within the view controller for the sheet.