在'applicationDidBecomeActive'之后调用什么方法? [英] What method is called after 'applicationDidBecomeActive'?

查看:268
本文介绍了在'applicationDidBecomeActive'之后调用什么方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一个问题,因为我在开发我的第一个iOS应用时遇到了问题。它是成千上万的手电筒应用程序之一,但我正在尝试尽可能多的功能。其中一个是在应用程序进入后台或终止时保存应用程序的状态。转到前台(iOS 4或更高版本)或重新启动后,我正在从文件加载设置并重新应用它们。其中一个设置显然是 AVCaptureDevice.torchMode 。但是,我遇到了这个问题。我正在 applicationDidBecomeActive 方法中重新应用这些设置。这一切似乎都有效,但是当我快速点击主页按钮然后重新启动应用程序时,应用程序将执行以下操作(我推迟了 applicationDidBecomeActive 方法来观察它) :


1。显示黑屏(加载)
2。执行 applicationDidBecomeActive 并打开LED(我把延迟放在这里)
3。显示我当前的 UIViewController 并在同一时刻关闭LED


仅在从后台调用应用程序后才会发生被送到那里。我知道这不是现实的用例场景,但我喜欢认为错误经常堆积,并且由于这种(可能)糟糕的设计,我将来可能会遇到其他问题。我绝对相信这不是我的代码关闭LED因为我 NSLog 每当我的代码修改 AVCaptureDevice.torchMode 属性。所以,确切地说,我的问题是:

applicationDidBecomeActive 之后调用什么方法,可能与 UIViewController <相关/ code>,这可能会关闭我的火炬?是否有任何可能的解决方案或解决方法?

it's my first question here as I have a problem developing my first iOS app. It is one of the thousands of flashlight apps, however I'm trying to put as many features as possible to it. One of them is saving the state of the app when it goes to background or terminates. After going to foreground (iOS 4 or higher) or relaunching, I'm loading the settings from file and reapplying them. One of the settings is, obviously, the AVCaptureDevice.torchMode. However, I encounter the problem with this. I'm reapplying these settings in the applicationDidBecomeActive method. It all seems to work, but when I very quickly tap the home button followed by relaunching an app, the app will do the following (I delayed the applicationDidBecomeActive method to observe it):

1. shows black screen (loading)
2. executes applicationDidBecomeActive and turns on the LED (I put my delay here)
3. shows my current UIViewController and at the same moment turns off the LED

It only happens after calling an app from background right after it was sent there. I know it's not the realistic use-case scenario, but I like to think that bugs often "stack up" and due to this (possibly) bad design I might encounter other problems in future. I am absolutely sure this is not my code turning the LED off since I NSLog whenever my code modifies the AVCaptureDevice.torchMode property. So, to be exact, my question is:
What method is called after applicationDidBecomeActive, possibly related to the UIViewController, that could turn my torch off? And is there any possible solution or work-around to it?

推荐答案

根据iOS应用编程指南


返回前台是您的应用程序有机会重新启动它移动到后台时停止的任务
。移动到前台时发生
的步骤如图3-6所示。
applicationWillEnterForeground:方法应该撤消在applicationDidEnterBackground:方法中完成
的任何操作,并且
applicationDidBecomeActive:方法应该继续执行相同的
激活任务将在发布时

您是否尝试在 applicationDidBecomeActive中重新应用您的设置: 方法而不是 applicationWillEnterForeground:

Have you tried re-applying your settings in the applicationDidBecomeActive: method instead of the applicationWillEnterForeground: ?

另一件需要考虑的事情是使用通知:

Another thing to consider is working with notifications:

applicationDidBecomeActive: applicationDidBecomeActive:方法对于AppDelegate,您可以告诉您的应用代表向您的控制器发送通知:

In the applicationDidBecomeActive: or applicationDidBecomeActive: methods of the AppDelegate, you can tell your app delegate to dispatch notifications to your controllers:

- (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.
     */

    // Dispatch notification to controllers
    [[NSNotificationCenter defaultCenter] postNotificationName: @"didBecomeActive" 
                                                        object: nil 
                                                      userInfo: nil];
}

一旦你有了这个,视图控制器可以注册这些通知(在他们的init方法,例如)像这样:

Once you have this, the view controller can register for these notifications (in their init method, for example) like so:

[[NSNotificationCenter defaultCenter] addObserver: self 
                                                 selector: @selector(loadSettings) 
                                                     name: @"didBecomeActive" 
                                                   object: nil];

这样,您的控制器就会知道应用程序刚刚变为活动状态,并且可以执行您想要的任何方法。

This way, your controller is aware that the app just became active and can execute any method you want.

在此示例中,您告诉视图控制器在收到<$ c时执行 loadSettings 方法$ c> didBecomeActive 通知(由应用代表发布)。

In this example, you are telling your view controller to execute the loadSettings method when it receives the didBecomeActive notification (which was posted by the app delegate).

这篇关于在'applicationDidBecomeActive'之后调用什么方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆