41

我打算在我的應用程序中實現多任務。 我在這裏可以看到很多的方法來做到這一點的AppDelegateapplicationWillResignActiveapplicationDidEnterBackgroundapplicationWillEnterForeground,...在AppDelegate中使用背景/前景方法

但是....我不認爲他們應該被使用的方式,也不知道爲什麼他們不在ViewControllers ...也不是他們在這裏。

我的意思是:當應用程序在後臺輸入時,我不知道我的用戶是哪個視圖。 然後,當應用程序進入前臺時,我將如何知道要做什麼以及我可以調用哪些內容來更新視圖?

如果這些方法其中每個視圖控制器,但在這裏,我沒有看到他們可以在一個具體的方式用於...

你能幫助我理解的方式,我會理解將這些方法實施到這些方法中?

回答

128

每個對象接收UIApplicationDidEnterBackgroundNotification通知時,應用程序在進入背景。所以當應用程序在雲後臺運行一些代碼,你只需要聽,你想要的通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(appHasGoneInBackground:) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 

不要忘記釋放偵聽器時,你不需要聽它了:

[[NSNotificationCenter defaultCenter] removeObserver:self]; 

而且最好的最好的,你可以用下面的通知發揮同樣的方式:

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification

玩得開心! :-)

如果這個答案幫助你,我會很高興地收到了給予好評;-)

8

它們不在您的任何視圖控制器中,因爲iOS採用了'委託'設計模式,您可以確信在需要時方法會觸發類(在此例中爲應用程序的應用程序委託) 。

作爲一個學習過程,爲什麼不把NSLog放在這些方法中看看它們何時被解僱呢?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  

    // Override point for customization after application launch. 
    NSLog(@"didFinishLaunchingWithOptions"); 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 


- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    /* 
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    */ 
    NSLog(@"applicationWillResignActive"); 
} 


- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, called instead of applicationWillTerminate: when the user quits. 
    */ 
    NSLog(@"applicationDidEnterBackground"); 
} 


- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background. 
    */ 
    NSLog(@"applicationWillEnterForeground"); 
} 


- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
    NSLog(@"applicationDidBecomeActive"); 
} 


- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    /* 
    Called when the application is about to terminate. 
    See also applicationDidEnterBackground:. 
    */ 
    NSLog(@"applicationWillTerminate"); 
} 
+0

我明白,當他們被解僱了,我不明白的是我會是什麼具體能夠寫入這些方法,因爲我不知道哪個是活動的ViewController。 – Oliver 2011-01-31 07:47:02

+2

沒有「主動」視圖控制器,這是OO編程。要問的更好的問題是; 「當我的單一應用程序代表被觸發時,我如何確保重要的類瞭解它?」。那是一個設計問題,而不是編程問題。例如,您可能讓Controller對象監聽在這些應用程序委託方法中觸發的Notifications,或者您可能直接將它們傳遞給Controller。 – 2011-02-01 21:54:31