gpt4 book ai didi

macos - NSWindowController/NSDocument 生命周期(关闭)

转载 作者:行者123 更新时间:2023-12-03 17:21:10 25 4
gpt4 key购买 nike

我有一个使用 NSWindowController、NSDocument 等的基于“标准”OS X 文档的应用程序。它有一个 NSTextView 作为其 UI 的一部分。

我的问题(这让我抓狂)是如何最好地捕获“关闭文档”,然后告诉 NSTexView 完成编辑。

完成编辑可能会导致模型被更新(并且可能会导致文档的更改计数),因此我需要在所有其他 NSDocument 逻辑决定是否需要保存之前执行此操作。

谢谢

最佳答案

您的 TextView 不允许撤消。

-(void)setAllowsUndo:(BOOL)value;

您可以通过调用以前的方法来禁用它,或者在界面生成器中取消选中它(对于 TextView )。如果 TextView 中没有撤消支持,您的文档不会自动变脏(不会进行 updateChangeCount,因此文档不知道它已被编辑)。

更干净的解决方案(不允许撤消):

要解决此问题,您必须注册为委托(delegate)并实现 textDidChange: (NSTextViewDelegate 方法)或注册 NSTextDidChangeNotification。在这些方法中,您可以更新模型和/或使文档变脏/编辑。

或者:

当尝试关闭窗口/文档时,捕获其中一个事件。并辞去响应者。 NSTextView 将失去焦点并更新其支持值。

[window makeFirstResponder:nil];

捕获关闭的可能方法列表。

- (void)close;  //you have to call [super close]; 
- (BOOL)windowShouldClose:(NSWindow *)window;
- (void)canCloseDocumentWithDelegate:(id)delegate shouldCloseSelector:(SEL)shouldCloseSelector contextInfo:(void *)contextInfo;

NSTextViewDelegate继承自NSTextDelegate

@protocol NSTextViewDelegate <NSTextDelegate>
@optional
@end

@protocol NSTextDelegate <NSObject>
@optional
- (BOOL)textShouldBeginEditing:(NSText *)textObject; // YES means do it
- (BOOL)textShouldEndEditing:(NSText *)textObject; // YES means do it
- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
- (void)textDidChange:(NSNotification *)notification; // Any keyDown or paste which changes the contents causes this
@end

关于macos - NSWindowController/NSDocument 生命周期(关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com