gpt4 book ai didi

objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?

转载 作者:可可西里 更新时间:2023-11-01 17:02:05 25 4
gpt4 key购买 nike

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

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

只有在应用程序发送到那里后立即从后台调用应用程序后才会发生。我知道这不是现实的用例场景,但我喜欢认为错误经常“堆积”并且由于这种(可能)糟糕的设计,我将来可能会遇到其他问题。我绝对确定这不是我关闭 LED 的代码,因为每当我的代码修改 AVCaptureDevice.torchMode 属性时,我都会使用 NSLog。所以,准确地说,我的问题是:
applicationDidBecomeActive 之后调用什么方法(可能与 UIViewController 相关)可以关闭手电筒?是否有任何可能的解决方案或解决方法?

最佳答案

根据iOS App Programming guide :

Returning to the foreground is your app’s chance to restart the tasks that it stopped when it moved to the background. The steps that occur when moving to the foreground are shown in Figure 3-6. The applicationWillEnterForeground: method should undo anything that was done in your applicationDidEnterBackground: method, and the applicationDidBecomeActive: method should continue to perform the same activation tasks that it would at launch time.

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

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

在 AppDelegate 的 applicationDidBecomeActive:applicationDidBecomeActive: 方法中,您可以告诉您的应用委托(delegate)向您的 Controller 发送通知:

- (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];
}

一旦有了这个, View Controller 就可以像这样注册这些通知(例如,在它们的 init 方法中):

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

这样,您的 Controller 就会知道该应用程序刚刚激活并可以执行您想要的任何方法。

在此示例中,您告诉 View Controller 在收到 didBecomeActive 通知(由应用委托(delegate)发布)时执行 loadSettings 方法。

关于objective-c - 'applicationDidBecomeActive' 之后调用了什么方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8387048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com