开发者

Reloading view when returning from background

Using iPhone SDK开发者_如何学JAVA 4.1. Sometimes when returning from the background state on iPhone 3GS device the view returned to has lost one of its images or labels. The viewDidAppear method doesn't get called either when returning from the background. Is there any way to force reloading the view so these methods are called?


This usually happens if your app receives a memory warning as a result of trying to store too much data in ram (in this case images).

To test whether this is the case you could either do an NSLog call inside the didReceiveMemoryWarning message or you could subclass UIImage and extend its dealloc and put an NSLog message inside it saying something like "Image being dealloced" and then check what gets written to the console. If you want to check it without being in the debugger (and therefore no console) you could create a debug UILabel inside the mainwindow xib at the very front (so its always visible) whose text value is set instead of writing to NSLog. This way youll be able to see what happened even after you return to your program.

Your best bet is the didReceiveMemoryWarning together with a UILabel object.

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  debugLabel.text=@"Did receive memory warning";
}

To solve the problem (i.e. to reload the images) you could register that view to receive the UIApplicationWillEnterForegroundNotification from the notification center and then call the necessary reloading calls i.e. check which images are nil (have been released) and should be reloaded.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewNeedsReload) name:UIApplicationWillEnterForegroundNotification object:nil];

- (void) viewNeedsReload
{
  //Check validity of each image here and reload if necessary
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜