xcode UIApplicationWillEnterForegroundNotification 越来越多地被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9261796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:18:05  来源:igfitidea点击:

UIApplicationWillEnterForegroundNotification get called more and more

iphonexcodensnotificationcenter

提问by user1175122

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!

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

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
  1. 关闭和打开:函数调用 1 次(这就是我想要的方式)
  2. 关闭和打开:函数调用 2 次
  3. 关闭和打开:函数调用 3 次
  4. 关闭和打开:函数调用 4 次

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.

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

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.

我使用的是 Xcode 4.2 和 iOS 5!我也用过UIApplicationDidBecomeActive,但它也会导致同样的问题。

回答by Brad Goss

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.

我不知道您何时将 self 添加为观察者,但是每次应用程序变得活跃时,您似乎一次又一次地将 self 添加为观察者。因此多次调用。

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.

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

回答by Nickolay Olshevsky

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

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

回答by user1175122

No i solved my problem.

不,我解决了我的问题。

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

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

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

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

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

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

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

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

回答by Mahendra Vishwakarma

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

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

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