I'm a newbie with Swift and iOS, so be gentle, please. 😉 (I have 20+ years of writing GUI-based Java apps and extensive design background with C++, so I'm not a newbie at programming in general.) I hope I haven't provided too much detail here. I'm trying to be concise, but also trying to explain what bases I've already covered.
I'm try to parse the documentation for UIResponder.undoManager found here because it doesn't seem to be working for me: https://developer.apple.com/documentation/uikit/uiresponder/1621122-undomanager
It sounds like there's a runtime stack of undoManager objects maintained by the UIResponder code (a superclass of UIView and thus part of all views). This stack should be searchable by simply invoking self.undoManager:
"When you request an undo manager, the request goes up the responder chain and the
UIWindow
object returns a usable instance."So if I have a view controller that is at the top of a UINavigationController stack (i.e. is the currently visible view), I should be able to access
self.undoManager and that will invoke my superclass's computed property, which walks the runtime stack looking for the relevant UndoManager object.
I'm not seeing that behavior. Here's my structure:
UITabBarController at the top (here is where I override undoManager and return a private UndoManager object; I also override canBecomeFirstResponder to return true)
viewDidAppear() calls beginFirstResponder() (which, as I understand it, adds the current object to the runtime stack, described above)
-- UINavigationController (activated by storyboard segue for the currently selected tab)
-- -- UITableViewController (root view controller for the nav controller; this is where I access self.undoManager)
When I breakpoint in my tab bar controller, I can see the private UndoManager object being created and I see my computer property's get method being called. But when the table view controller access undoManager (inherited from UIResponder), it always returns nil.
It looks like the UIResponder property isn't searching the runtime stack properly, or maybe there's some other method that I need to call or implement to activate all of this. I was hoping I could find the source code for UIResponder somewhere (part of Swift being open source, right?) but I can't find any of the library code. I'm guessing Apple is doing the same thing that Microsoft did with C# -- supporting the language on other platforms, but not the libraries?
I appreciate any insight you might have! Thanks!