4

当我的应用程序被中断时,例如接听电话、屏幕锁定或切换应用程序,我需要它根据中断时屏幕上的视图/视图控制器做出不同的响应。

在我的第一个视图控制器中,我们称之为 VCA,我有这个

              [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(doSomething)
 name:UIApplicationWillResignActiveNotification 
 object:NULL];

    -(void)doSomething{
    //code here
    };

在VCB我有

    [[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(doSomethingElse)
 name:UIApplicationWillResignActiveNotification 
 object:NULL];

    -(void)doSomethingElse{ //code here };

但是如果 VCB 在屏幕上,或者任何后续的视图控制器(vcc,vcd,vce),并且屏幕被锁定,它只会响应 VCA 中定义的 doSomething 方法。即使我在 VCA 之后的视图控制器之一中没有 UIApplicationWillResignActiveNotification,它仍然会响应 VCA 中定义的 doSomethign 方法。

有什么方法可以让我的应用程序根据进入后台时屏幕上的哪个视图做出不同的响应?

4

3 回答 3

2

这在applicationDidEnterBackground中对我有用

if ([navigationViewController.visibleViewController isKindOfClass:[YourClass class]]) {
    //your code
}
于 2012-06-13T03:14:45.043 回答
0

你是说你的 doSomethingElse 函数永远不会被调用吗?你确定这一点,也许除了 doSomething 之外它还被调用?我认同。

在这种情况下,在 doSomething 和 doSomethingElse 中,您可以在第一行添加一个检查以忽略当前未加载的通知:

if ([self isLoaded] == NO)
   return;
于 2012-06-13T03:06:30.373 回答
0

收到通知后检查当前的 visibleViewController 怎么样?如果它与您的接收器匹配而不是执行操作,否则忽略它。

于 2012-06-13T03:55:38.463 回答