Reputation: 1337
I am teaching myself objective C, following the book: The big Nerd Ranch guide and my app is crashing although I am copying straight from the book and only three lines into the code. Here is the code that is crashing it.
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect windowFrame = UIScreen.mainScreen.bounds;
UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
[self setWindow:theWindow];
return YES;
}
The book has been known to have typos and another thought is that maybe some of this is deprecated? Any help most welcome. Thanks P
EDIT-----------------------------------------------------------------------------------------------------------------------------
The line that is crashing the app is:
[self setWindow:theWindow];
and the reason for crashing:
'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
I am just wondering if maybe views used to be set up in the appDelegate and not anymore and this is why the code that used to work is now crashing?
Upvotes: 1
Views: 2253
Reputation: 1
I think that the idea it's that you need to have a view controller at the end of the executino of the AppDelegate so the app will show something to the user.
You need to set an initial view controller on a Storyboard ('Application windows are expected to have a root view controller at the end of application launch' --> your error message)
For example:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"YOURSTORYBOARDNAME" bundle:nil];
UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];
I hope this will be useful.
Upvotes: 0
Reputation: 682
You should not have to ever set the window unless you decide to use multiple windows. With the introduction of storyboards, just set your first View Controller as your root controller, and this View Controller will load automatically
Upvotes: 0
Reputation: 176
You are setting the window correctly. The problem you are having is that you need assign a UIViewController to the rootViewController property of your window at some point before reaching the end of the didFinishLaunchingWithOptions: method.
ie.
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect windowFrame = UIScreen.mainScreen.bounds;
UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
theWindow.rootViewController = viewController;
[self setWindow:theWindow];
return YES;
}
@end
Upvotes: 1