I'm working on adding a mac build target to an iOS application. I have the basics working and would like to implement undo/redo functionality.
In a traditional AppKit application you get this for free. When you create a new application, the prepopulated main menu has the Edit item and Undo and Redo under it. The view controller has an undoManager
, you just registerUndo
on it (preferably setActionName
as well) and everything works. Hotkeys, menu item title changes and state changes (disable redo when at the top of the stack etc.) all work out of the box.
Adding a catalyst build target to an iOS project also creates a default menu with a top level Edit menu and Undo/Redo menu items. These do not seem to adopt the built-in functionality. Do I really need to manually recreate all that is free with AppKit or is there something I'm missing?
Registering undo on NSWindow.undoManager
works for me. But I had to use a hidden/private API to access the NSWindow
instance using Dynamic library:
let nsWindow = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(view.window)
let undoManager: UndoManager? = nsWindow.undoManager
I was able to get this working in my Catalyst app only by calling becomeFirstResponder()
in viewDidAppear
and no sooner. I was originally calling it in viewDidLoad
and also tried viewWillAppear
, neither of which correctly register my undo manager with macOS.
Relevant view controller sample code:
class MyViewController: UIViewController {
override var undoManager: UndoManager? {
return myCustomUndoManager
}
override var canBecomeFirstResponder: Bool {
true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
becomeFirstResponder()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With