无法模拟调用 applicationDidReceiveMemoryWarning:?

发布于 2024-10-22 02:32:23 字数 361 浏览 1 评论 0原文

以下是调用代码:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
                                                    object:[UIApplication sharedApplication]];

它无法从 UIApplicationDelegate 调用 applicationDidReceiveMemoryWarning:

有什么问题吗?

Here is the calling code:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
                                                    object:[UIApplication sharedApplication]];

It can not invoke applicationDidReceiveMemoryWarning: from UIApplicationDelegate.

Anything wrong?

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

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

发布评论

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

评论(3

星星的軌跡 2024-10-29 02:32:23

在模拟器中,使用菜单触发内存不足警告。

在此处输入图像描述

In the simulator, use the menu to trigger a low memory warning.

enter image description here

失与倦" 2024-10-29 02:32:23

您无法通过发布通知来模拟内存警告。 UIApplicationDidReceiveMemoryWarningNotification 在收到内存警告时由 UIApplication 发布,但它没有观察到它,因此不会调用您的视图当您手动发布此类通知时,控制器的 didReceiveMemoryWarning: 方法。

正如 Rob 已经指出的那样,您可以使用“模拟内存警告”菜单项在 iOS 模拟器中模拟内存警告。

您还可以在视图控制器中观察通知,而不是实现 didReceiveMemoryWarning:,但我不建议这样做,因为当您伪造内存警告时,系统提供的视图控制器的行为可能会有所不同那样。

You cannot simulate a memory warning by posting a notification. The UIApplicationDidReceiveMemoryWarningNotification is posted by UIApplication when it receives a memory warning, but it doesn't observe it, and therefore doesn't call your view controllers' didReceiveMemoryWarning: method when you post such a notification manually.

As Rob already pointed out though, you can simulate a memory warning in the iOS simulator by using the "Simulate Memory Warning" menu item.

You could also observe the notification in your view controllers, instead of implementing didReceiveMemoryWarning:, but I wouldn't recommend that, because the behavior of system-supplied view controllers might be different when you fake a memory warning that way.

坚持沉默 2024-10-29 02:32:23

应该有效的是使用 UIApplicationMemoryWarningNotification 而不是 UIApplicationDidReceiveMemoryWarningNotification

[[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification"
        object:[UIApplication sharedApplication]];

What should work is using UIApplicationMemoryWarningNotification instead of UIApplicationDidReceiveMemoryWarningNotification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification"
        object:[UIApplication sharedApplication]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文