Start of program

  1. When you open a project with Xcode, it’s easy to find a file called main.m, which is the entry point for the application.
  2. When the program starts, it executes the main function, which is the entry point for iOS programs
  3. The UIApplicationMain function is called internally
  4. UIApplicationMain is going to create a UIApplication object
  5. Then create a DELEGATE object for the UIAPPlication
  6. Then the AppDelegate, starts a message loop (main runloop) that notifies the AppDelegate whenever it listens for a corresponding system event
int main(int argc, char * argv[]) {
    @autoreleasepool {
        returnUIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}Copy the code

A UIApplication object is a symbol of an application. Each application has its own UIApplication object, which is singleton. This singleton object is available through [UIApplication sharedApplication]. The first object created when an iOS application is launched is a UIApplication object, which can be used to perform some application-level operations.

The UIApplicationMain function is implemented as follows

int UIApplicationMain {
    int argc,
    char *argv[],
    NSString *principalClassName,
    NSString *delegateClassName
}
Copy the code

The first argument is the number of arguments that the program takes before entering mian, which defaults to 1. The second argument is the array that loads the function, which defaults to the name of the program. The third argument is the UIApplication class name or its subclass name, which defaults to nil. The fourth argument is the name of the protocol UIApplicationDelegate instantiation object, which is the method that the UIApplication object tells it to execute when it listens for changes to the system.

In UIApplicationMain, depending on the UIApplication name passed in and the name of its proxy, it mainly does the following:

  • Create a UIApplication object based on the name passed in
  • Create UIApplication proxy objects based on the passed proxy name
  • Start the event loop (if not, the program ends after main. Make sure the program lasts after it is created.
  • Plist: the Main storyboard file base name Key has a Value. If there are values, said after back through the Storyboard load controller, will receive the didFinishLaunchingWithOptions APPDelegate) (start to finish, the Storyboard will be carried out in a series of load operation; If there is no value, is not through the storyboard load controller, then get didFinishLaunchingWithOptions news in AppDelegate) (start to finish, this time we need to load controller by means of the code.

When it’s done, the didFinishLaunching method is called, and in that method UIWindow is created, the AppDelegate window property is set, and the root controller of UIWindow is set

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *viewController = [[UIViewController alloc] init]; self.window.rootViewController = viewController; // The view of the root controller has not been added to self.window [self.window makeKeyAndVisible]; // The view of the root controller is added to self.windowreturn YES;
}
Copy the code

First create the window and get a proper UIWindow instance object to display the interface (self.window is a built-in property of the system). Next, set the root controller for the window. Create your own controller and set it to the root controller of self.window. Note that the view from the root controller has not yet been added to self.window. The view from the root controller is added to the window when it is ready to display. Display window:

[self.window makeKeyAndVisible] // Actually does the followingCopy the code

First set self.window to UIApplication’s keyWindow so that we can later see which UIApplication’s main window is. Next, make self.window visible, which is equivalent to executing code:

self.window.hidden = NO;
Copy the code

So since makeKeyAndVisible does the above, actually replace [self.window makeKeyAndVisible] with self.window. Hidden = NO, then the interface will also display normally, Because that’s what happens inside makeKeyAndVisible. But I’m not setting UIApplication’s keyWindow at this point, so it’s better to use makeKeyAndVisible for easy access later.

UIWindow supplement

Windows are hierarchical and can have multiple Windows at the same time. For example, the status bar is a window, and the keyboard is a window. You can adjust the hierarchy by setting the windowLevel property of UIWindow objects. self.window.windowLevel = UIWindowLevelStatusBar; Window has three levels: UIWindowLevelNormal, UIWindowLevelStatusBar, and UIWindowLevelAlert. If three levels of colleagues appear on the screen, alert is at the top, statusBar is in the middle, and Normal is at the bottom. Note: If there are multiple Windows in an application, the controller hides the status bar by default. Solution: Turn off controller control of the status bar (add View Controller-based Status bar appearance key to info.plist and set it to NO) so that the Windows and status bar can be displayed hierarchically.

An overview of

  1. We start with main, and inside main we call UIApplicationMain
  2. What does UIApplicationMain do
    • Create UIApplication object
    • Create a UIApplication delegate object — AppDelegate
    • Start a message loop: Notify the AppDelegate every time a corresponding system event is listened on
    • Create a UIWindow object (inherited from UIView) for the application, set to the Window property of the AppDelegate
    • Load the info.plist file and read the name of the main storyboard file
    • Load the main storyboard file and create the controller object indicated by the white arrow
    • And set the controller created in the previous step to the rootViewController property of UIWindow (root controller)
    • Showing UIWindow, before showing it, it will add the View of rootViewController to UIWindow (it will create the view of the controller at this step)
[window addSubview:window.rootViewController.view];
Copy the code