How to use NSUndoManager on macOS 10.12?
Create an undo manager and assign it to the managed object context.
NSUndoManager *undoManager = [[NSUndoManager alloc] init];
managedObjectContext.undoManager = undoManager;
How do I register UndoManager in Swift?
I tried this in a Playground and it works flawlessly:
class UndoResponder: NSObject {
@objc func myMethod() {
print("Undone")
}
}
var undoResponder = UndoResponder()
var undoManager = UndoManager()
undoManager.registerUndo(withTarget: undoResponder, selector: #selector(UndoResponder.myMethod), object: nil)
undoManager.undo()
Capturing UndoManager from SwiftUI environment
It looks more natural for SwiftUI to use the following approach
var body: some View {
TopLevelView(document: document, undoManager: undoManager)
}
and
struct TopLevelView: View {
@ObservedObject var document : MyDocument
var undoManager: UndoManager?
init(document: MyDocument, undoManager: UndoManager?) {
self.document = document
self.undoManager = undoManager
self.setUndoManager(undoManager: undoManager)
}
// ... other code
}
How can I replace text in UITextView with NSUndoManager support?
There is actually no reason for any hacks or custom categories to accomplish this. You can use the built in UITextInput Protocol method replaceRange:withText:
. For inserting text you can simply do:
[textView replaceRange:textView.selectedTextRange withText:replacementText];
This works as of iOS 5.0. Undo works automatically and there are no weird scrolling issues.
Creating a continuously updating custom control with proper undo management
Can't you just use beginUndoGrouping
and endUndoGrouping
?
P.S. If you're curious how Cocoa classes work internally, it can sometimes be helpful to check out the GNUstep implementions (NSUndoManager is in GNUstep Base, AppKit classes like NSSliderCell are in GNUstep GUI):
http://wwwmain.gnustep.org/resources/downloads.php?site=http%3A%2F%2Fftpmain.gnustep.org%2Fpub%2Fgnustep%2F#core
Related Topics
Should I Invalidate Timer When My Application Is Suspended or in Background
Why Are Image Views Sometimes Not Appearing in Collection View Cells
Tab Bar Item Icons Appear Darker
How to Center Nspopover When Using Swiftui
How to Custom The Image of Mkannotation Pin
Restrictions Around Protocols and Generics in Swift
Why Is There Multiple Collision Calls Sprite Kit Swift
How to Properly Map JSON Properties to Model Properties in Realm.Create
Create Objects/Instances in Variables Swift
Swift: Method Overriding in Parameterized Class
Execute Code on Main Thread from Async F#
Swift - Nsurl Fileurlwithpath Not Unwrapped
Cannot Assign Value of Type 'Menuview' to Type 'some View'
iOS - Could Not Open Obj File When Convert Mdlasset to Mdlmesh