iPhone应用程序-内存警告后背景消失

iPhone应用程序-内存警告后背景消失,iphone,objective-c,memory-leaks,Iphone,Objective C,Memory Leaks,当我在基于导航的iPhone应用程序中收到内存警告时,先前分配的导航控制器中的背景图像将消失 背景图像设置为UIView的背景属性。然后将此视图添加到应用程序代理的主窗口: UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame]; backgroundView.backgroundColor=[UIColor colorWithPatternImage:[[Utilities sharedManager]get

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

背景图像设置为UIView的背景属性。然后将此视图添加到应用程序代理的主窗口:

UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
backgroundView.backgroundColor=[UIColor colorWithPatternImage:[[Utilities sharedManager]getImageWithName:@“Hintergrund2”]; backgroundView.tag=56789; [窗口添加子视图:背景视图]; [背景视图发布]

[window addSubview:[navigationController view]];

[window makeKeyAndVisible];
在我收到内存警告信息并按下导航控制器的后退按钮后,先前的导航控制器显示正常的苹果灰色背景。我必须导航回我的主屏幕并向前导航以“重置”背景图像

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

提前感谢您的帮助

问候


Phil

在内存不足的情况下,操作系统如何处理直接添加到主
ui窗口的内容尚不明确


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

首先,您可能应该将控制器添加到窗口中,如下所示:

window.rootViewController = navigationController;
我尚未对此进行测试,但您可以直接设置窗口的背景色:

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

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

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

- (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
,并创建了一个新视图,但该视图的背景颜色设置为默认颜色,但不清晰。

navigationController.view.backgroundColor=[UIColor WithPatternImage:[[Utilities sharedManager]getImageWithName:@“Hintergrund2”]];