简体   繁体   中英

Modal View controller dismissed on memory warning?

I have an intermittent bug that happens in my app and I was finally able to consistently reproduce it by simulating a memory warning in a certain scenario. Here's the setup.

 Nav controller
 (as tab inside      --> Modal Controller 1 --> Modal Controller 2
 Tab Controller)

If I simulate a memory warning while Modal Controller 2 is on the screen, Modal Controller 1 is dismissed and the bottom navigation controller pops all the way back to its root. This is exactly what happens in the reported bug.

If the user then presses the Done button in MC2, it invokes a delegate method in MC1, which crashes because the memory warning caused MC1 to be unloaded and dealloc'd.

There are other places in my app where I have navigation controllers presenting chains of modal view controllers, yet triggering a memory warning will only cause this problem in one particular area of my app.

Is there anything that could cause this behavior? I don't have specific code samples because I'm not sure what code is causing the problem. I'd appreciate any pointers at all.

When a memory warning happens, iOS may release any invisible views, including MC1's view (but not MC1 itself) in your case. You need to implement the following in MC1.

  • in viewDidUnload or before showing MC2, remember the current state.
  • in viewDidLoad, restore the old state if any.
  • in delegate method, call [self view] to load the view if it's released.

Or, you may want to consider the following hack. UIViewController prevent view from unloading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM