Nsundomanager: Capturing Reference Types Possible

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



Leave a reply



Submit