Objective c 如何在cocoa中持久化应用程序状态
我需要在应用程序终止时存储应用程序的状态,这样当用户重新运行应用程序时,应用程序将从上次关闭时的状态运行。这是一种还原应用程序,但在应用程序意外关闭时调用还原方法。但我需要恢复应用程序时,每次它意外关闭的用户手动关闭它。 我只需要存储应用程序界面,而不是应用程序的数据。 任何想法都会对我有帮助。 谢谢 如果要保存NSWindow位置,可以使用Objective c 如何在cocoa中持久化应用程序状态,objective-c,cocoa,persistent,nswindowrestoration,Objective C,Cocoa,Persistent,Nswindowrestoration,我需要在应用程序终止时存储应用程序的状态,这样当用户重新运行应用程序时,应用程序将从上次关闭时的状态运行。这是一种还原应用程序,但在应用程序意外关闭时调用还原方法。但我需要恢复应用程序时,每次它意外关闭的用户手动关闭它。 我只需要存储应用程序界面,而不是应用程序的数据。 任何想法都会对我有帮助。 谢谢 如果要保存NSWindow位置,可以使用[window saveFrameUsingName:@“myWindow”] 并使用 [window setFrameAutosaveName:@“myW
[window saveFrameUsingName:@“myWindow”]代码>
并使用
[window setFrameAutosaveName:@“myWindow”]代码>@应用程序启动。您可以通过以下任何可用方法保持状态:
i<代码>NSUserDefaults
例如:
//saving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"testBool"];
//retrieving
[defaults boolForKey:@"testBool"];
NSMutableDictionary *stateDictionary = [NSMutableDictionary dictionary];
//set state
...
//saving
[stateDictionary writeToFile:<filePath> atomically:YES];
//retrieve
stateDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:<filePath>]
ii。正在序列化状态对象。
iii.另存为plist文件
例如:
//saving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"testBool"];
//retrieving
[defaults boolForKey:@"testBool"];
NSMutableDictionary *stateDictionary = [NSMutableDictionary dictionary];
//set state
...
//saving
[stateDictionary writeToFile:<filePath> atomically:YES];
//retrieve
stateDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:<filePath>]
NSMutableDictionary*stateDictionary=[NSMutableDictionary];
//设定状态
...
//拯救
[stateDictionary writeToFile:原子:是];
//取回
stateDictionary=[NSMutableDictionary dictionaryWithContentsOfFile:]
iv.使用sqlite或核心数据
(除非
应用程序的状态处于某种对象关系模型中)
更新:
为了保存windows的UI状态,
选中此项,在标题下的用户界面保存我知道此方法,它在应用程序终止时调用。我的问题是,如何存储应用程序状态(UI控件的位置)。strore UI控件在NSUserDefaults中的位置您可以使用saveFrameUsingName:保存NSWindow位置setFrameAutosaveName:您可以解释如何使用这些来存储添加到NSWindow上的控件框架吗。请不要告诉我存储每个控件的框架,因为我在窗口上有很多控件,存储每个控件框架是一项非常艰巨的任务。不幸的是,NSWindow不能直接序列化或写入文件。您可以使用子类NSWindow覆盖编码方法(请参阅列表项ii的链接),也可以检查我在更新的答案中提供的链接。