Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS self.window - when is it created?

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.

like image 794
Blaszard Avatar asked Jan 27 '14 12:01

Blaszard


People also ask

What is window in iOS?

The Role of the Window in an iOS App. A Window's Root View Contains Your Content. The Key Window Receives Certain Events. Most App Windows Exist at the Same Level. A Window Is Associated with One Device Display.

What is UI window?

Answer: U.I. Stands for United Inches. When you measure your window always measure the width first and the height second — add the two figures together and that number is the total united inches. (Example 36 X 36 =3D 72 U.I.) Note: Always round off measurements to the next highest inch.

Is only one window available in Iphone?

Yes, you can have multiple windows. A key window is the one who receives the user input.

What is Uiwindow in objective c?

The backdrop for your app's user interface and the object that dispatches events to your views.


1 Answers

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.

like image 136
Fogmeister Avatar answered Sep 22 '22 07:09

Fogmeister