应用程序崩溃“无法恢复以前选定的框架”消息

时间:2012-02-19 12:30:14

标签: objective-c ios xcode uiview

我无法弄清楚为什么代码导致应用程序崩溃。

AppDelegate.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.rootViewController = [[[RootViewController alloc]init]autorelease];

    [self.window setRootViewController:self.rootViewController];


    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

这是RootViewController.m代码

-(void)loadView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
    [view setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:view];
    [view release];
}

我在调试器中得到了该消息

Unable to restore previously selected frame.

Here is screenshot

2 个答案:

答案 0 :(得分:3)

loadView应该设置视图。当self.view为零时调用它。现在,您正在调用[self.view addSubview:view]; UIKit调用loadView,这会创建无限递归。你应该在这里做self.view = view;

答案 1 :(得分:1)

loadView负责首先设置视图。你错过了那样做。相反,你为self.view添加了一个视图。

按以下行更改代码:

self.view = view;

而不是[self.view addSubview:view];

同样建议在从函数返回之前调用[super loadView]