How to avoid data loss when UIImagePickerController unloads my controller?

I use UIImagePickerController to take a photo from a camera. However, I find that my calling controller (the one shown before the UIImagePickercontroller is displayed) is randomly unloaded. I registered viewDidUnload, and indeed, it is called. When the camera is completed and fired, my viewDidLoad dispatcher will be called, unfortunately, all the state has now disappeared. Things like typed text or selected items disappear.

Obviously, this is due to exhaustion of memory. But is this behavior normal? Should I handle it? This is not typical of how modalViewController works. Usually you show it and reject, everything should be intact.

What is a good way to avoid data loss in this case? Do I have to write a bunch of code to keep the full state?

+3
source share
3 answers

It is not recommended to override -didReceiveMemoryWarningso that the UIViewController cannot free the view, because then you may encounter a "real" low memory problem, and your application will be forced to leave the system.

What you really need to do is save the text and other bits of input from the view in your method -didReceiveMemoryWarning(be sure to call super). Then in -viewDidLoadyou can put the text and other bits back into the user interface.

, , . , - !

+2

, didReceiveMemoryWarning, , , .

, , , , .

, didReceiveMemoryWarning UIViewController, [super didReceiveMemoryWarning], , , .

+3

My solution to the same problem described above was to declare an instance variable in my view controller that saves the contents UITextViewif the view is unloaded. Thus, in my method, viewWillDissapearI save the contents UITextView.textin my instance variable, and in my method viewWillAppearI restore the contents.

- (void)viewWillAppear:(BOOL)animated
{
    if (messageTextString != nil)
    {
        textEdit.text = messageTextString;
    }

    [super viewWillAppear:animated];
}
  • (voids) viewWillDisappear: (BOOL) animated {self.messageTextString = textEdit.text;

    [super viewWillDisappear: animated]; }

0
source

Source: https://habr.com/ru/post/1723737/


All Articles