2013-01-05 42 views
1

我正在使用我的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]; 
    } 
} 

做這樣的事情最好的方法是什麼?有沒有可能是我不知道的一個簡單的技巧?

謝謝!

+0

我不認爲它甚至有可能在iOS的... – iProgrammed

+0

作爲替代,這將是可愛的彈出一個'UIActivityIndi​​cator'到當前視圖,而我加載某些對象回記憶。但我已經嘗試了'UIViewController * currentVC = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]''和'UIViewController * currentVC = [[UIApplication sharedApplication] keyWindow] .rootViewController;'試圖獲取該VC,似乎工作。 –

回答

0

你的意思是你想讓應用程序在每次啓動時重新啓動(或輸入前景)?如果是的話,也許你可以設置應用不支持多任務處理

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW5

搜索「選擇退出後臺執行的」

============= ================================================== =================

對不起,我不知道你的應用程序的性質,如果一些過程需要在後臺運行,那麼這種方法是不走。

我在上面閱讀了關於UIActivityIndi​​cator的註釋,並在應用程序輸入前景後將某些對象加載回當前視圖。也許這線程可以幫助你,Finding the current view when application enter foreground. IOS

+0

這將是一個很好的解決方案,除了(我沒有提到的小細節-sorry),這個應用程序的主要功能是在後臺運行。這是一個音頻應用程序,但有一些沉重的視覺元素,可以在低內存的情況下發布。所以當我的內存不足時,我想發佈一些圖像,但保留可能在那個時候播放的音頻,而不是讓iOS關閉整個事情。然後,當應用程序重新啓動時,我想回到啓動頁面,以便可以重新加載這些可視元素。合理? –

相關問題