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.
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.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.