6

我的应用程序使用大量内存。通常它运行良好,但在一段时间未重新启动的已加载设备上,它将因臭名昭著的内存不足错误而被抛弃。

我想响应didReceiveMemoryWarning并释放我的一些缓存。

但我的问题是我的应用程序基于 OpenGL ES 模板并且没有视图控制器。它只有一个 App Delegate,它包含对 glView 的引用。

我可以做些什么来捕获didReceiveMemoryWarning消息以便我可以响应?

4

2 回答 2

10

您还可以在任何您想要的类中将方法作为观察者添加到UIApplicationDidReceiveMemoryWarningNotification通知中。代码可能像这样:

- (void) cleanMemory: (NSNotification*) notification {
  // Save memory!
}

- (id) init {  // Or any other function called early on.
  // other init code
  [[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(cleanMemory:)
          name:UIApplicationDidReceiveMemoryWarningNotification
        object:nil];
  return self;
}
于 2009-08-12T19:18:23.350 回答
9

这在您的Application Delegate中也可用。

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  NSLog(@"Received memory warning!");
}
于 2009-08-12T17:59:54.800 回答