应用程序崩溃并显示“无法恢复先前选择的帧”信息

发布于 2025-01-07 13:39:08 字数 975 浏览 0 评论 0原文

我不明白为什么该代码会导致应用程序崩溃。

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

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.

Here is screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2025-01-14 13:39:08

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

loadView is supposed to set the view. It is called when self.view is nil. Now you're calling [self.view addSubview:view]; UIKit calls loadView, and that creates an infinite recursion. You're supposed to do self.view = view; here.

随风而去 2025-01-14 13:39:08

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

通过以下行更改代码:

self.view = 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:

self.view = view;

instead of [self.view addSubview:view];

Also it is advisable to call [super loadView] before returning from the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文