views:

152

answers:

3

I have read about this function didReceiveMemoryWarning that actually hasn't really helped. I would like to show a UIAlert View to tell the user that the action he is about to take will lead to problems with memory.

So apart from crashing, which is a nasty way to inform the user that there is a memory Warning received, is there a possible implementation of a UIAlertView?

+3  A: 

In your application delegate class (eg MyApplicationAppDelegate.m) implement the didReceiveMemoryWarning method:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  // Show an alert
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                  message:@"Running low on memory"
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];

  [alert show];
  [alert release];
}
pheelicks
Does this prevent the app from crashing after the user presses "OK"?
erastusnjuki
No. All it does is it shows an alert. To prevent crashing you have to do something to regain some memory, eg get rid of unused objects and views
pheelicks
A: 

the action he is about to take will lead to problems with memory

If there is some action you know of the user taking that will lead to memory problems, you should keep them from taking that action, or just warn them yourself when they are about to take the action (with an alertview).

cmcculloh
This is the only way out?
erastusnjuki
Well, it looks like what you are asking for is for the iPhone OS to pre-analyze any code that is about to run (on the fly) as a result of a user action, and before running that code determine (again, on the fly) that by running the code a memory issue will occur. I'm not 100% sure, but I'd be shocked if there was a way to make this happen. This is why Apple has guidelines for good coding practices and memory management. If OSes could "look ahead" or "look into the future" and determine that by running some code a bug would occur, nothing would ever crash ever.
cmcculloh
+1  A: 

Pheelicks did give you a good answer to your question, but this is definetly not what you want to do. When you receive this warning, you are already in low memory condition. What you want to do when you receive this warning is release as much memory as possible. Like large images that you might be keeping in memory, large arrays of string or any other large object. Instruments will help you alot in finding the culprits.

Also, you also want to implement didReceiveMemoryWarning on any view controller that allocates alot of memory so they can do some cleanin up there also

Hopes this helps :)

MLefrancois