如何模拟applicationWillResignActive消息?

发布于 2024-12-17 12:43:59 字数 439 浏览 4 评论 0原文

如何模拟 applicationWillResignActive 被调用?

锁定屏幕、进入主菜单、模拟电话——似乎都没有帮助。

如果我预计事情不会发生,请让我告诉您更多信息:我订阅此消息,当发生这种情况时,希望将通知发送到如下所列的方法:

UIApplication *app = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(applicationWillResignActive:) 
                 name: UIApplicationWillResignActiveNotification object:app];

How can one simulate applicationWillResignActive to be called?

Locking screen, going to main menu, simulating phone call - none seemed to have helped.

In case I expect things that won't happen, let me tell you more: I subscribe to this message and when that happens hope that notification is sent to a method as listed below:

UIApplication *app = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(applicationWillResignActive:) 
                 name: UIApplicationWillResignActiveNotification object:app];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2024-12-24 12:43:59

你的代码是有效的,它对我有用。锁定屏幕或点击主页按钮将导致发布此通知。

*(需要注意的是,如果您的设备不支持多任务处理,或者您在 *info.plist应用程序不在后台运行”属性设置为 yes > 在这种情况下,它将直接进入“UIApplicationWillTerminateNotification”通知)

因此,除非有两种可能性:

1)您的addObserver代码没有被调用, IE。这是用了错误的方法。

要测试,请尝试以下操作:

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillResignActive:) 
                                             name:UIApplicationWillResignActiveNotification 
                                           object:app];
NSLog(@"Observer added");

2)观察者方法未正确调用。这需要与 at 选择器相同的方法。

要测试,请尝试以下操作:

-(void)applicationWillResignActive:(NSNotification *)notification{
    NSLog(@"applicationWillResignActive");
}
  • 作为附加点,如果您想查看实际调用了哪些通知以及何时调用。在您的 AppDelegate 类中,将行 NSLog(@"%@", NSStringFromSelector(_cmd)); 放入每个 -application... 方法中,它将记录它们当他们被召唤时。这是了解他们的好方法。

Your code is valid, it works for me. And locking the screen or hitting the home button will cause this notification to be posted.

*(One caveat to this is if your device does not support multitasking or if you have the "Application does not run in background" property set to yes in your *info.plist. In which case it will go straight to the "UIApplicationWillTerminateNotification" notification)

So barring that there there are two possiblities:

1) Your addObserver code is not being called, ie. It's in the wrong method.

To test try this:

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillResignActive:) 
                                             name:UIApplicationWillResignActiveNotification 
                                           object:app];
NSLog(@"Observer added");

2) The observer method is not being called properly. Which requires the same method as the at selector.

To test try this:

-(void)applicationWillResignActive:(NSNotification *)notification{
    NSLog(@"applicationWillResignActive");
}
  • Just as an additional point, if you want to see in action which of these notifications are called and when. In your AppDelegate class put the line NSLog(@"%@", NSStringFromSelector(_cmd)); in each of the -application... methods and it will log them when they are called. It's a nice hands on way to get to know them.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文