Question

when you start your app using single view template, and you add the NSLog(@"self.window = %@", self.window); in your first line of the AppDelegate.m's application: didFinishLaunchingWithOptions: method, you can see that self.window exists in your app.

However, when you start your app using empty template, and tried to log the self.window to the console, the result returns null. Even if you add storyboard and a view controller, and set its view controller as the initial view controller, and attempt to log the self.window, the result is the same - its value is set to null.

And note that whichever way you take, you can find you declare @property (strong, nonatomic) UIWindow *window; in AppDelegate.h by default. So I wonder why in the first case, you can see that self.window is initialized and set the value but not in the latter case. Also, if self.window is already declared and initialized in the first case but NOT in the second case, how can I find the initialization code?

It looks like in both cases, the @property declaration is same - and in both cases, as I mentioned, I tried to log the value of self.window in the FIRST LINE of the AppDelegate.m's application: didFinishLaunchingWithOptions: method.

So anything that I'm missing? I don't know why those two cases act differently despite me not finding any differences in both code and storyboard.

I use iOS 7 and Xcode 5. Thanks.

Was it helpful?

Solution

OK, when you create a project with a Storyboard or Nib then the project settings will tell the project that the storyboard/nib is the "Main Interface".

This triggers the application to load that interface on start up. This is why the self.window is created in these cases.

When you create an empty application there is no interface to set as the main interface.

You then need to create the window yourself like this...

-(void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     UIViewController *someController = [UIViewController... //create your initial controller

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
}

Something like this anyway. It's been a while.

Alternatively, if you create an empty application and then add a nib file that you want to use as the initial nib then you can select it in the project settings.

In the Target in General. In the section "Deployment Info" select the "Main Interface" from the nibs in your project. This will then load that nib when the application starts.

OTHER TIPS

Xcode declares UIWindow as IBOutlet Object in Appdelegate and xcode itself hooks or connect it with the window of default ViewController.nib(Created by Xcode when you create non empty project).There is no need to initialise any object if you have declared it as Iboutlet and connected it with any UIController in nib.

Now in empty project if you want to create window declare it as outlet and connect it with window in exist in nib and make your AppDelegate as Files OWner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top