2

我正在使用最新的 SDK 开发一个 iOS 应用程序。

这是一个全屏应用程序。

我有一个方法viewWillAppear,每次应用程序来自后台时都必须调用它。

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self setUpVideo];
}

setUpVideo设置了,AVCaptureVideoPreviewLayer因为当应用程序从后台返回时我丢失了视频。

正如我所读到的,viewWillAppear当应用程序从后台返回时不会调用它,现在,我不知道将代码放在哪里。

在这个问题上,oculus建议使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];,但它对我不起作用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpVideo:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

有什么建议吗?

4

3 回答 3

4

UIApplicationWillEnterForegroundNotification改为观察。

- (void)viewDidAppear {
    [super viewDidAppear];
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(enterForeground:) 
              name:UIApplicationWillEnterForegroundNotification 
              object:nil];
    // ...
}

- (void)enterForeground:(NSNotification *)notification {
    // do stuff
}

不要viewWillAppear:直接从enterForeground:方法调用。viewWillAppear:而是将所有必需的代码移动到一个单独的方法中,并从和中调用它enterForeground:

于 2013-03-21T10:30:28.277 回答
1
applicationWillEnterForeground will trigger when app comes from background

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

此外,您可以 UIApplicationDidBecomeActiveNotification用于触发某些方法

[[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(handleMethod:)
                                                 name: UIApplicationDidBecomeActiveNotification
                                               object: [UIApplication sharedApplication]];
于 2013-03-21T10:30:33.890 回答
0

尝试从 - (void)applicationDidBecomeActive:(UIApplication *)applicationAppDelegate 发布此通知(或观察更好的相应通知)

于 2013-03-21T10:29:55.077 回答