App crash with “Unable to restore previously selec

2019-09-02 20:13发布

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

2条回答
\"骚年 ilove
2楼-- · 2019-09-02 20:34

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.

查看更多
看我几分像从前
3楼-- · 2019-09-02 20:44

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.

查看更多
登录 后发表回答