cocoa - RestoreStateWithCoder 未在 NSView 中触发

标签 cocoa nsview restore

我已经子类化了 nsview 并想要实现恢复功能。在我退出应用程序之前,编码代码运行:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
     [coder encodeObject:[NSValue valueWithRect:self.originalFrame] forKey:@"originalFrame"];
    [super encodeRestorableStateWithCoder:coder];
}

再次启动应用程序时,restoreStateWithCoder 永远不会被调用?

最佳答案

我刚刚遇到了同样的问题,并在 NSUserInterfaceItemIdentification protocol reference 中找到了解决方案:

Identifiers are used during window restoration operations to uniquely identify the windows of the application. You can set the value of this string programmatically or in Interface Builder. If you create an item in Interface Builder and do not set a value for this string, a unique value is created for the item when the nib file is loaded. For programmatically created views, you typically set this value after creating the item but before adding it to a window.

我确实以编程方式创建了 View ,因此没有为其设置标识符,并且窗口恢复机制没有调用 View 恢复方法。

在将 View 添加到窗口之前,您需要设置一个标识符,如下所示:

_exampleView.identifier = @"ExampleIdentifier";

如果您的 View 随后调用[self invalidateRestorableState],系统将在适当的时间调用encodeRestorableStateWithCoder,一切都会按预期进行。

关于cocoa - RestoreStateWithCoder 未在 NSView 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921118/

相关文章:

installation - 从命令 : ado dir 生成的列表中重新安装软件包

macos - 搜索字符串谓词

objective-c - 单元测试、STAssertThrows 和 NSExceptions

cocoa - 为什么当我切换 View 时,acceptFirstResponder 会被调用两次?

objective-c - NSScrollview 与 NSGradient

objective-c - 分层 Cocoa WebView - 在顶部绘图?

sql-server - 无需事务日志即可恢复 SQL Server 数据库

objective-c - 打印 NSDocument

cocoa - 检测同一 NSView 中两个对象的接近度

git - 通过已知文件夹名称恢复丢失的提交