无法模拟调用 applicationDidReceiveMemoryWarning:?
以下是调用代码:
[[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在模拟器中,使用菜单触发内存不足警告。
In the simulator, use the menu to trigger a low memory warning.
您无法通过发布通知来模拟内存警告。
UIApplicationDidReceiveMemoryWarningNotification
在收到内存警告时由UIApplication
发布,但它没有观察到它,因此不会调用您的视图当您手动发布此类通知时,控制器的didReceiveMemoryWarning:
方法。正如 Rob 已经指出的那样,您可以使用“模拟内存警告”菜单项在 iOS 模拟器中模拟内存警告。
您还可以在视图控制器中观察通知,而不是实现
didReceiveMemoryWarning:
,但我不建议这样做,因为当您伪造内存警告时,系统提供的视图控制器的行为可能会有所不同那样。You cannot simulate a memory warning by posting a notification. The
UIApplicationDidReceiveMemoryWarningNotification
is posted byUIApplication
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.应该有效的是使用
UIApplicationMemoryWarningNotification
而不是UIApplicationDidReceiveMemoryWarningNotification
:What should work is using
UIApplicationMemoryWarningNotification
instead ofUIApplicationDidReceiveMemoryWarningNotification
: