iOS 处理内存警告

发布于 2025-01-04 21:22:10 字数 552 浏览 1 评论 0原文

我有一个 UIViewController ,它显示为模式视图控制器。在它的视图中,我有一个 UITextView、4 个 ImageButtons(带有 CustomType)和另一个按钮(也是自定义类型),它允许拍照然后在该按钮中显示它。

接下来的问题是:

  1. 假设我在 UITextView 中设置了一些文本,并且 4 个 ImageButton 中每个都有一个图像。
  2. 然后我启动 UIImagePickerController 并收到内存警告(通常会发生这种情况),因此调用 didReceiveMemoryWarning 并 viewDidUnload。
  3. 当我从 UIImagePickerController 返回时,所有信息都被清除,因此用户必须重新输入所有内容(并为按钮设置 4 张照片)。

所以问题是,我应该如何处理这种情况以避免用户重新输入所有内容?我应该将状态保存在 UserDefaults 中还是将旧信息存储在 UIViewController 的属性中,以便在再次调用 viewDidLoad 时使用它们?

谢谢!

I have a UIViewController that is shown as a modal view controller. In its view I have an UITextView, 4 ImageButtons (with CustomType) and another button (custom type too) which allows to take a photo and then show it in that button.

The problem is next:

  1. Suppose I have set some text in the UITextView and the 4 ImageButtons have an image in each one of them.
  2. Then I launch the UIImagePickerController and I receive a memory warning (what happens usually) so didReceiveMemoryWarning gets called and viewDidUnload.
  3. When I return from UIImagePickerController all the info is cleaned so the user has to retype everything (and set the 4 photos for the buttons).

So the question is, how should i handle this situation in order to avoid user retyping everything? Should i save the state in UserDefaults or store the old info in the properties of the UIViewController so use them when the viewDidLoad gets called again?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这样的小城市 2025-01-11 21:22:10

没有理由涉及 NSUserDefaults。只需将各种控件的状态保存在视图控制器的属性中即可。然后,当视图重新加载时,您可以恢复 -viewDidLoad 方法中的控件。

There's no reason to involve NSUserDefaults. Just save the state of the various controls in your view controller's properties. Then you can restore the controls in your -viewDidLoad method when the view is reloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文