简体   繁体   English

iPhone-处理内存具有多个视图

[英]iPhone - Handling Memory with Multiple Views

Here is a piece of code from the exploring the iPhone SDK book. 这是探索iPhone SDK书中的一段代码。 It uses the example of 2 views. 它以2个视图为例。 It checks to see which view is being used and will release the other one. 它检查查看正在使用哪个视图,并将释放另一个视图。

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning]; // Releases the view if it
    // doesn't have a superview

    // Release anything that's not essential, such as cached data
    if (self.blueViewController.view.superview == nil)
        self.blueViewController = nil;
    else
       self.yellowViewController = nil;
}

My question is: If my app has many views eg 15, would I need to check each view as above and release what's not being used? 我的问题是:如果我的应用程序有很多视图,例如15,我是否需要如上所述检查每个视图并释放未使用的视图? Or is there a different technique used in that case?` 还是在这种情况下使用了不同的技术?

The concept is what is more important from the book (releasing anything nonessential in the case of a low memory warning) rather than the details of the example (purging the view you aren't using). 从书中来看,概念是更重要的(在内存不足警告的情况下释放不必要的内容),而不是示例的细节(清除您不使用的视图)。

In the case of the many views your application is using my first question would be how often are the views being switched? 在有很多视图的情况下,您的应用程序正在使用我的第一个问题:视图切换的频率是多少? If the user is staying on one view or another for a while it might be best to free up the other views as you transition them off the screen to preemptively give your app as much free memory as possible. 如果用户停留在一个视图或另一个视图上一段时间,则最好在释放其他视图时将其释放,以抢先为您的应用提供尽可能多的可用内存。 In this case the book's example really wouldn't apply as you'd already have all the 'cached' views purged. 在这种情况下,本书的示例实际上将不适用,因为您已经清除了所有“缓存”视图。 If they are quickly switching between views then the off-screen views are good to keep around (setting up a new view is somewhat expensive.) In this case you'll need to follow the book's example in a low memory event. 如果它们在视图之间快速切换,那么屏幕外的视图就可以很好地保持(设置新的视图会有些昂贵。)在这种情况下,在内存不足的情况下,您需要按照本书的示例进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM