问题描述
我计划在我的应用中实现多任务.我可以在这里看到许多方法在 AppDelegate
中做到这一点,例如 applicationWillResignActive
、applicationDidEnterBackground
、applicationWillEnterForeground
,...
I'm planning to implement multi-task in my app.I can see many methods here to do that in the AppDelegate
like applicationWillResignActive
, applicationDidEnterBackground
, applicationWillEnterForeground
, ...
但是......我不明白它们应该被使用的方式,也不明白为什么它们不在 ViewControllers 中......也不明白它们在这里的用途.
But.... I don't see the way they should be used, nor why they are not in the ViewControllers... Nor what they are here for.
我的意思是:当应用程序进入后台时,我不知道我的用户在哪个视图上.然后,当应用程序进入前台时,我如何知道要做什么以及我可以调用什么,例如更新视图?
I mean : when the app enter in background, i don't know on which view my user is.And back, when the app comes into foreground, how would I know what to do and what I may call, to update the view for example ?
我会理解这些方法是否在每个视图控制器中,但在这里,我没有看到它们可以具体用于什么......
I would have understood if those methods where in each view controller, but here, I don't see what they can be used for in a concrete way...
你能帮我理解在这些方法中实现事物的方式吗?
Can you help me to understand the way to implement things into those methods ?
推荐答案
当应用程序进入后台时,每个对象都会收到一个 UIApplicationDidEnterBackgroundNotification
通知.因此,要在应用程序进入后台时运行一些代码,您只需在需要的地方收听该通知:
Each object receive a UIApplicationDidEnterBackgroundNotification
notification when the app goes in background. So to run some code when the app goes in background, you just have to listen to that notification where you want :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appHasGoneInBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
当您不再需要收听时,不要忘记释放收听者:
Don't forget to release the listener when you don't need to listen to it anymore :
[[NSNotificationCenter defaultCenter] removeObserver:self];
最好的,您可以通过以下通知以相同的方式进行游戏:
And best of the best, you can play the same way with the following notifications :
UIApplicationDidEnterBackgroundNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification
UIApplicationDidBecomeActiveNotification
这篇关于AppDelegate 中背景/前景方法的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!