gpt4 book ai didi

objective-c - 如何在 cocoa 中保留应用程序状态

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

我需要在应用程序终止时存储应用程序的状态,以便当用户重新运行应用程序时,应用程序从上次关闭的状态运行。这是某种恢复应用程序,但当应用程序意外关闭时调用恢复方法。但每次当用户手动关闭它时,我都需要恢复应用程序。我只需要存储应用程序用户界面而不是应用程序的数据。任何想法都会对我有帮助。谢谢

最佳答案

您可以使用任何可用的方法来保留状态,例如:

我。 NSUserDefaults

示例:

//saving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"testBool"];
//retrieving
[defaults boolForKey:@"testBool"];

ii.序列化状态对象。

iii.保存为 plist 文件

示例:

NSMutableDictionary *stateDictionary = [NSMutableDictionary dictionary];
//set state
...

//saving
[stateDictionary writeToFile:<filePath> atomically:YES];

//retrieve
stateDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:<filePath>]

iv.使用 sqlite 或 Core-Data

(很可能不需要,除非 您的应用程序的状态处于某种对象关系模型中)

更新:
为了保留窗口的 UI 状态, 检查这个link并在用户界面保护标题下。

关于objective-c - 如何在 cocoa 中保留应用程序状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18439162/

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