我正在使用我的appDelegate的applicationDidReceiveMemoryWarning
方法來轉儲某些高級資源對象。當應用程序開始備份時,而不是嘗試僅重新加載這些對象並將用戶返回到他的最後一頁,我只想從頂部重新啓動應用程序(從主頁面開始,應用程序僅深入一層,所以這是完全可以接受的)。低內存警告後重新啓動應用程序
這是我的微不足道的嘗試,但它是一個可怕的失敗。它接近了,但我最終介紹了導致實際內存轉儲和應用程序崩潰的一些問題。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[SplashScreenViewController alloc] initWithNibName:@"SplashScreenViewController" bundle:nil];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//For testing purposes only
self.lowMemoryWarning = TRUE;
NSLog(@"app did enter background");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"app will enter foreground");
if (self.lowMemoryWarning) {
NSLog(@"recovering from low memory warning");
self.window.rootViewController = nil;
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
}
}
做這樣的事情最好的方法是什麼?有沒有可能是我不知道的一個簡單的技巧?
謝謝!
我不認爲它甚至有可能在iOS的... – iProgrammed
作爲替代,這將是可愛的彈出一個'UIActivityIndicator'到當前視圖,而我加載某些對象回記憶。但我已經嘗試了'UIViewController * currentVC = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]''和'UIViewController * currentVC = [[UIApplication sharedApplication] keyWindow] .rootViewController;'試圖獲取該VC,似乎工作。 –