iOS 处理内存警告
我有一个 UIViewController ,它显示为模式视图控制器。在它的视图中,我有一个 UITextView、4 个 ImageButtons(带有 CustomType)和另一个按钮(也是自定义类型),它允许拍照然后在该按钮中显示它。
接下来的问题是:
- 假设我在
UITextView
中设置了一些文本,并且 4 个 ImageButton 中每个都有一个图像。 - 然后我启动 UIImagePickerController 并收到内存警告(通常会发生这种情况),因此调用 didReceiveMemoryWarning 并 viewDidUnload。
- 当我从 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:
- Suppose I have set some text in the
UITextView
and the 4 ImageButtons have an image in each one of them. - Then I launch the
UIImagePickerController
and I receive a memory warning (what happens usually) sodidReceiveMemoryWarning
gets called and viewDidUnload. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由涉及 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.