4

I have the following code in my App Delegate file:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MBFeedViewController *feedViewController = [[MBFeedViewController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:feedViewController];
    [self.window makeKeyAndVisible];
    return YES;
}

Then when I run my app, the console outputs the message:

Application windows are expected to have a root view controller at the end of application launch

Can't figure this one out. FeedViewController is not nil when I set the window's RootViewController.

Cesare
  • 9,139
  • 16
  • 78
  • 130
NSNolan
  • 1,025
  • 1
  • 13
  • 18

1 Answers1

3

Can't figure this one out. feedViewController is not nil when I set the window's rootviewcontroller.

Make sure that you're properly initializing your view controller. It's most common to use the designated initializer, -initWithNibName:bundle:. H2CO3 points out that init is okay too. Either way, make sure that you're also initializing the superclass by calling [super initWithNibName:... bundle:...] or just [super init].

Next, make sure that feedViewController.view isn't nil. When you set the window's root view controller, the window will install that controller's view as its own content. A view controller normally creates its view the first time it's view property is accessed, so there's no reason that you should ever get nil there unless creating the view fails.

Finally, try instantiating a plain old UIViewController and setting that as the window's root view controller. Do you get the same warning? If yes, perhaps you've stumbled on a bug. If no, look carefully at MBFeedViewController, especially the initializer(s), -loadView, -viewDidLoad and other methods that are called early in the life of the view controller.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 2
    Using `init` with `UIViewController` is just fine. It calls `initWithNibName:nil bundle:nil`. –  Mar 09 '13 at 07:52
  • Start a new single-view project without using storyboards, go to app delegate implementation and change `self.viewController = [[XXViewController alloc] initWithNibName:bundle:]` to `[[XXViewController alloc] init]`. You won't get a view, but also you won't get an error output. – anticyclope Mar 09 '13 at 07:55
  • @H2CO3 Didn't realize that. It's not documented on the UIViewController reference page, but I see that the header file says: *(As a convenience, the default init method will do this for you, and specify nil for both of this methods arguments.)* – Caleb Mar 09 '13 at 07:58
  • @Caleb The essence is in the details ;-) –  Mar 09 '13 at 07:58
  • @H2CO3 That kind of thing is the reason I stick around here. – Caleb Mar 09 '13 at 08:01
  • feedViewController.view isn't nil and I am calling [super init] in ffeedViewController's init method – NSNolan Mar 09 '13 at 08:06
  • Also I am not using nib files. – NSNolan Mar 09 '13 at 08:07
  • @NSNolan You can still call `-initWithNibName:bundle:` if you're not using a nib file -- just pass `nil` for the name and bundle parameters. If we believe the comment in UIViewController.h you shouldn't have to change that, but it might be worth a try. – Caleb Mar 09 '13 at 08:15
  • @NSNolan Added a few more ideas above. – Caleb Mar 09 '13 at 08:23
  • @Caleb, your ideas lead me to figure out that it is an asynchronous network call in my viewDidLoad method that is causing this message to prompt itself. Do you know of a reason why that would be the case? – NSNolan Mar 09 '13 at 08:30
  • @NSNolan, are you sure that's the cause? I tried to duplicate this, with a long running async process in viewDidLoad, but couldn't get your error message. – rdelmar Mar 09 '13 at 17:23