43

アプリにマルチタスクを実装することを計画しています。私はここでそれを行うための多くの方法を見ることができますAppDelegate、、、、applicationWillResignActive.. ..applicationDidEnterBackgroundapplicationWillEnterForeground

しかし....私はそれらがどのように使用されるべきか、またなぜそれらがViewControllersにないのか...またそれらが何のためにここにあるのかわかりません。

つまり、アプリがバックグラウンドで入力されたときに、ユーザーがどのビューにいるかわかりません。そして戻って、アプリがフォアグラウンドになったとき、たとえばビューを更新するために、何をすべきか、何を呼び出すことができるかをどのように知ることができますか?

これらのメソッドが各ViewControllerにあるかどうかは理解できたはずですが、ここでは、具体的に何に使用できるかわかりません...

それらのメソッドに物事を実装する方法を理解するのを手伝ってもらえますか?

4

2 に答える 2

132

アプリがバックグラウンドになると、各オブジェクトはUIApplicationDidEnterBackgroundNotification通知を受け取ります。したがって、アプリがバックグラウンドになったときにコードを実行するには、必要な場所でその通知をリッスンするだけです:

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

もう聞く必要がない場合は、リスナーを解放することを忘れないでください。

[[NSNotificationCenter defaultCenter] removeObserver:self];

そして何よりも、次の通知で同じようにプレイできます :

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification
于 2011-09-07T22:04:08.643 に答える
8

iOS は「デリゲート」デザイン パターンを採用しているため、必要に応じてメソッドがクラス (この場合はアプリケーションの App Delegate) で確実に起動されるため、それらはどのビュー コントローラーにもありません。

学習プロセスとして、これらのメソッドに 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");
}
于 2011-01-31T03:37:10.647 に答える