gpt4 book ai didi

cocoa - RestoreStateWithCoder 未在 NSView 中触发

转载 作者:行者123 更新时间:2023-12-03 16:37:06 27 4
gpt4 key购买 nike

我已经子类化了 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/

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