是否可以为一个 NSWindow 实现多个 NSUndoManager ?

发布于 2024-09-12 01:28:42 字数 152 浏览 8 评论 0原文

我有一个带有多个视图的窗口(它们都是 NSView 的子类,并且始终只有一个可见),我在其中绘制路径。我希望每个视图都有一个 NSUndoManager,但显然它们都有相同的来自 NSWindow 的 NSUndoManager。

这可能吗?

谢谢 克索尼克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖树树初阳… 2024-09-19 01:28:42

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

Check out the NSWindowDelegate method windowWillReturnUndoManager:. You should be able to use this to return the correct undo manager for the current view.

拧巴小姐 2024-09-19 01:28:42

// 这对我来说适用于具有两个不同数据源和撤消管理器的 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];
}

// This works for me on a NSTableView subclass with two different data sources and undo managers (in a document window)

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文