UIApplicationWillEnterForegroundNotification 被越来越多地调用

发布于 2025-01-05 05:01:25 字数 742 浏览 3 评论 0原文

我在视图控制器中使用此函数来识别应用程序是否再次处于活动状态并刷新一些数据:

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(becomeActive:)
                   name:UIApplicationWillEnterForegroundNotification
                 object:nil];

这适用于活动后刷新,但每次应用程序从后台返回到前台(非活动到活动)时,它都会调用该函数再一次。

因此,如果我关闭并打开应用程序 4 次,该函数将被调用 4 次!

编辑:函数将以这种方式调用:

  1. 关闭和打开:函数调用1次(这就是我想要的方式)
  2. 关闭和打开:函数调用2次
  3. 关闭和打开:函数调用3次
  4. 关闭而 open: 函数被调用了 4 次

,但返回前台后只需调用 1 次。在某些情况下,应用程序必须在激活并检查数据后显示警报视图。当该函数被调用 4 次时,该警报视图将显示 4 次。

在应用程序委托中,此函数不执行任何操作,但会被提及。

我正在使用 Xcode 4.2 和 iOS 5!我也使用了UIApplicationDidBecomeActive,但它也导致了同样的问题。

I am using this function in my view controllers to recognize if the app is active again and to refresh some data:

[[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(becomeActive:)
                   name:UIApplicationWillEnterForegroundNotification
                 object:nil];

This works for refreshing after getting active, but everytime the app gets back from background to foreground (inactive to active) it calls the function one more time.

So if I closed and opened the app 4 times, the function will be called 4 times!

EDIT: The function will be called this way:

  1. close and open: function called 1 time (that's the way i want it)
  2. close and open: function called 2 times
  3. close and open: function called 3 times
  4. close and open: function called 4 times

But it only have to be called 1 time after getting back in foreground. In some situation the app have to show an alert view after getting active and checking data. This alert view will be shown 4 times when the function will be called 4 times.

In the app delegate this function does nothing, but it is mentioned.

I am using Xcode 4.2 and iOS 5! I also used UIApplicationDidBecomeActive, but it also cause the same problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千年*琉璃梦 2025-01-12 05:01:26

这实际上就是这个通知应该做的事情。如果您需要在应用程序启动时收到通知,请使用 applicationDidFinishLaunching。

That's actually what this notification is supposed to do. If you need to be notified when application is started, use applicationDidFinishLaunching.

用心笑 2025-01-12 05:01:26

我认为你应该删除 viewWillDisappear 方法中的 addObserver 。它对我有用。

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

i think you should remove addObserver in viewWillDisappear method. it is working for me.

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
天煞孤星 2025-01-12 05:01:26

不,我解决了我的问题。

通过观察者调用的方法将不再调用viewDidLoad。因此 viewDidLoad 只会调用一次(在第一个应用程序启动时)。

将通过观察者调用的函数getActive现在调用首先从viewDidLoad调用的方法。

我还将 removeObserver 函数放入 viewDidLoad 中,只有当用户完全停止应用程序时才会调用该函数。

感谢您的想法和帮助!现在我对制作多任务应用程序有了更多了解。

No i solved my problem.

The method which is called through the observer will not call the viewDidLoad anymore. so the viewDidLoad will only called once (on first application start up).

The function getActive which will be called through the observer now calls the methods which were firstly called out of the viewDidLoad.

I also put the removeObserver function to the viewDidLoad, which will only be called, if the user will stop the app completely.

Thanks for the ideas and the help! Now i know a little bit more about making a multi-tasking app.

白芷 2025-01-12 05:01:25

我不知道你什么时候将自己添加为观察者,但每次应用程序激活时,你似乎都会一次又一次地将自己添加为观察者。因此多次调用。

您只能将视图控制器添加为观察者一次。尝试使用控制器 init: 方法。并且,确保在 dealloc: 方法中将视图控制器作为观察者删除。

I don't know at when your adding self as an observer, but every time the app becomes active, you seem to be adding self as an observer, again and again. Thus the multiple calls.

You must only add your view controller as an observer once. Try using the controllers init: method. And, ensure that you remove the view controller as an observer in the dealloc: method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文