应用程序崩溃并显示“无法恢复先前选择的帧”信息
我不明白为什么该代码会导致应用程序崩溃。
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.
I can't figure out why that code leads to app crash.
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;
}
Here is RootViewController.m code
-(void)loadView
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
[view setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:view];
[view release];
}
I get that message in the debugger
Unable to restore previously selected frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
loadView
应该设置视图。当 self.view 为零时调用它。现在,您正在调用[self.view addSubview:view];
UIKit 调用loadView
,这会创建无限递归。您应该在这里执行self.view = view;
。loadView
is supposed to set the view. It is called whenself.view
is nil. Now you're calling[self.view addSubview:view];
UIKit callsloadView
, and that creates an infinite recursion. You're supposed to doself.view = view;
here.loadView 负责首先设置视图。你错过了这样做。相反,您向 self.view 添加了一个视图。
通过以下行更改代码:
而不是
[self.view addSubview:view];
另外,建议在从函数返回之前调用
[super loadView]
。loadView is responsible to set the view first. you missed to do that. Instead you added a view to self.view.
Change the code by below line:
instead of
[self.view addSubview:view];
Also it is advisable to call
[super loadView]
before returning from the function.