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