Search code examples
iosobjective-cuiwindowuiapplication

How UIApplication knows when a UIWindow is created in the app?


In the UIApplicationDelegate's application:didFinishLaunchingWithOptions: I have the following code:

NSLog(@"%d\n", [UIApplication sharedApplication].windows.count);

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

NSLog(@"%d\n", [UIApplication sharedApplication].windows.count);

(mainWindow is a @property declared in the header)

The first NSLog prints out 0 while the second prints 1.

How UIApplication knows that a window is created and initialized in the application?


Solution

  • While we don't have the source code to be 100% sure, it would seem that the initializer of UIWindow is written to tell UIApplication that it has been created. It's simply part of Apple's implementation of the two classes.