2

I have a window with multiple views (they all subclass NSView and there is always only one visible) on which i draw paths. I'd like to have an NSUndoManager for each view, but obviously they all have the same NSUndoManager, coming from the NSWindow.

Is this even possible?

Thx xonic

4

2 回答 2

2

检查NSWindowDelegate方法windowWillReturnUndoManager:。您应该能够使用它为当前视图返回正确的撤消管理器。

于 2010-07-25T17:08:56.530 回答
0

// 这适用于具有两个不同数据源和撤消管理器的 NSTableView 子类(在文档窗口中)

- (void)awakeFromNib
{
    _undoManager1 = [NSUndoManager new];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(undoManagerNotification:) name:NSUndoManagerDidCloseUndoGroupNotification object:_undoManager1];
    _undoManager2 = [NSUndoManager new];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(undoManagerNotification:) name:NSUndoManagerDidCloseUndoGroupNotification object:_undoManager2];
}

- (NSDocument *)document
{
    return [NSDocumentController.sharedDocumentController documentForWindow:self.window];
}

- (void)undoManagerNotification:(NSNotification *)note
{
    // NSUndoManagerDidCloseUndoGroupNotification: we made a change
    [self.document updateChangeCount:NSChangeDone];
}

- (NSUndoManager *)undoManager
{
    if ( self.window.firstResponder != self )
        return self.window.firstResponder.undoManager;

    // returns the right undo manager depending on current data source  
    return dataSource == dataSource1 ? _undoManager1 : _undoManager2;
}

- (IBAction)undo:(id)sender
{
    [self.undoManager undo];
    [self.document updateChangeCount:NSChangeUndone];
}

- (IBAction)redo:(id)sender
{
    [self.undoManager redo];
    [self.document updateChangeCount:NSChangeDone];
}

- (void)setValue:(id)newValue
{
    [[self.undoManager prepareWithInvocationTarget:self] setValue:_myValue]; // NSUndoManager will post the NSUndoManagerDidCloseUndoGroupNotification
    _myValue = newValue;
}

- (IBAction)doChangeSomeValue:(id)sender
{
    [self setValue:someValue];
}
于 2019-02-28T11:33:13.127 回答