【问题标题】:App crash with "Unable to restore previously selected frame" message应用程序崩溃并显示“无法恢复先前选择的帧”消息
【发布时间】:2012-02-19 12:30:14
【问题描述】:

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

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.

【问题讨论】:

    标签: objective-c ios xcode uiview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      通过以下行更改代码:

      self.view = view;
      

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

      此外,建议在从函数返回之前调用[super loadView]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-04
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        相关资源
        最近更新 更多