【问题标题】:iPhone App - background disappears after memory warningiPhone App - 内存警告后背景消失
【发布时间】:2010-09-12 14:18:42
【问题描述】:

当我在基于导航的 iPhone 应用中收到内存警告时,背景图像会在之前分配的导航控制器中消失。

背景图片被设置为 UIView 的背景属性。然后将此视图添加到 App 委托的主窗口中:

UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];

backgroundView.backgroundColor = [UIColor colorWithPatternImage:[[Utilities sharedManager] getImageWithName:@"Hintergrund2"]]; 背景视图.tag = 56789; [窗口添加子视图:背景视图]; [背景视图发布];

[window addSubview:[navigationController view]];

[window makeKeyAndVisible];

在我收到内存警告信息并按下导航控制器的返回按钮后,之前的导航控制器显示正常的 Apple 灰色背景。我必须导航回主屏幕并向前导航以“重置”背景图像。

谁能告诉我为什么背景消失了?这不是因为图像,当我将背景设置为像 blackColor 这样的标准 UIColor 时,背景(颜色)也会消失。

提前感谢您的帮助!

问候

菲尔

【问题讨论】:

    标签: iphone objective-c memory-leaks


    【解决方案1】:

    在内存不足的情况下,操作系统如何处理直接添加到主 UIWindow 的内容是未定义的。

    也许考虑使用一个简单的UIViewController 作为背景?当内存不足时,您至少会得到回调。

    【讨论】:

    • navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[[Utilities sharedManager] getImageWithName:@"Hintergrund2"]];
    【解决方案2】:

    首先,您应该像这样将控制器添加到窗口中:

    window.rootViewController = navigationController;
    

    这个我没测试过,不过或许你可以直接设置窗口的背景颜色:

    window.backgroundColor = [UIColor colorWithPatternImage:[[Utilities sharedManager] getImageWithName:@"Hintergrund2"]];
    

    如果这不起作用,您将需要一种更好的方法来在内存不足的情况下重新创建视图状态。最标准的方法是在每个 xib 中定义背景,然后重新加载。或者,您可以通过查看您的 backgroundView 是否有超级视图来捕获内存不足警告并根据需要重新创建。

    【讨论】:

      【解决方案3】:

      确保您在视图控制器的- (void) viewDidLoad 中设置调用self.view.backgroundColor = [UIColor clearColor];,否则将不会显示窗口的图像。

      - (void) viewDidLoad
      {
          [super viewDidLoad];
      
          self.view.backgroundColor = [UIColor clearColor];
      ...
      }
      

      我的问题是我在初始化时设置了视图的backgroundColor

      MyTableViewController  *myTVC =
          [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
      myTVC.view.backgroundColor = [UIColor clearColor];
      

      而不是- (void) viewDidLoad

      在这种情况下,UITableViewController 的视图的背景颜色是在最初创建后设置的。然而,在内存不足警告之后,未显示的UIViewController 调用了- (void) viewDidUnload。这解除了他们的观点。就在UIViewController 再次显示之前,- (void) viewDidLoad 被调用并创建了一个新视图,但该视图的背景颜色设置为不清晰的默认值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多