IOS app loading process (from clicking the icon to display on the page)

Source: Internet
Author: User

IOS app loading process (from clicking the icon to display on the page)

Today, when I helped my colleagues solve the problem, I found that the cause of the program BUG was that my colleagues were not familiar with the process of starting and trying to load the program. So when there is no problem with the local code, but the program is not always the result we want, we should think about whether it is because we ignore the reason for trying to load the process. Below we will use an example to briefly introduce several common methods during the startup process. First, we created an attempt controller through XIB (the name is Empty. Don't ask me why it is called this name, because I'm too lazy and I won't go there when the mouse clicks fast ), A subclass of UIView (MyView) is created, and the Empty object is set as the window controller, and an object of MyView is set as the main view of Empty. OK. After learning about the above content, we can write the code.

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {NSLog (@ "% s" ,__ func __); // load the Controller Empty * test = [[Empty alloc] initWithNibName: @ "Empty" bundle: nil]; // create a window UIWindow * window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; self. window = window; // set the created controller to the root controller self. window. rootViewController = test; // activate and display the window [self. window makeKeyAndVisible]; return YES ;}

Since our applications are not directly created through the storyboard, we need to set our own controller. Before the application calls didfinishlaunchingwitexceptions:, it will first check whether the view controller can be created through the storyboard. If not, we need to manually create the window and controller. Since we want to know the execution process of each method, we need to add the print information of the method to each method. The Empty View Controller code is as follows:

# Import "MyView. h "# import" Empty. h "@ interface Empty () @ end @ implementation Empty // call the load method of each class when the program starts again. As the official saying goes, the load method of each class will be called no matter whether the class is loaded into the runtime. // Invoked whenever a class or category is added to the Objective-C runtime; + (void) load {NSLog (@ "% s" ,__ func __);} // this method is called only once before init. If a class creates 10 objects, init will execute 10 times, but this method will only execute once. + (Void) initialize {NSLog (@ "% s" ,__ func _);} // This is not too familiar-(instancetype) init {NSLog (@ "% s" ,__ func _); return [super init] ;}// whether it is-(instancetype) initWithCoder :( NSCoder *) aDecoder {NSLog (@ "% s" ,__ func _); return [super initWithCoder: aDecoder];} // this method is called when loading from nib-(instancetype) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {NSLog (@ "% s ", __func _); if (self = [super initWithNibN Ame: nibNameOrNil bundle: nibBundleOrNil]) {} return self;} // After initialization, the view is loaded. Note that after the controller is created, the view is not loaded, views are loaded lazily. Pay attention to them when using them. -(Void) loadView {NSLog (@ "loadview"); [super loadView]; UIView * view = [[MyView alloc] init]; view. backgroundColor = [UIColor yellowColor]; self. view = view;} // call when a constraint is triggered-(void) updateViewConstraints {NSLog (@ "% s" ,__ func _); [super updateViewConstraints];} // call a view after it is loaded. If the view is not destroyed and re-displayed, the view will only be executed once-(void) viewDidLoad {NSLog (@ "% s ", __func _); [super viewDidLoad];} // The view will be displayed soon-(void) viewDidAppear :( BOOL) animated {[super viewDidAppear: animated]; NSLog (@ "% s" ,__ func _);} // The view is about to display-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; NSLog (@ "% s" ,__ func __);}

The above is the Controller class, and I have mentioned it in the code. The following is the code in MyView.

# Import "MyView. h "@ implementation MyView // drawing-(void) drawRect :( CGRect) rect {NSLog (@" % s ",__ func _);} + (void) load {NSLog (@ "% s" ,__ func _);} // set the layout for the subview. Generally, frame-(void) layoutSubviews {[super layoutSubviews]; NSLog (@ "% s" ,__ func _);} @ end

After reading the code, let's take a look at the printed information.

2015-04-21 00:26:00.123 Runloop[19756:1191521] +[Empty load]2015-04-21 00:26:00.124 Runloop[19756:1191521] +[MyView load]2015-04-21 00:26:00.309 Runloop[19756:1191521] -[AppDelegate application:didFinishLaunchingWithOptions:]2015-04-21 00:26:00.309 Runloop[19756:1191521] +[Empty initialize]2015-04-21 00:26:00.309 Runloop[19756:1191521] -[Empty initWithNibName:bundle:]2015-04-21 00:26:00.310 Runloop[19756:1191521] loadview2015-04-21 00:26:00.314 Runloop[19756:1191521] -[Empty viewDidLoad]2015-04-21 00:26:00.314 Runloop[19756:1191521] -[Empty viewWillAppear:]2015-04-21 00:26:00.315 Runloop[19756:1191521] -[MyView layoutSubviews]2015-04-21 00:26:00.315 Runloop[19756:1191521] -[MyView layoutSubviews]2015-04-21 00:26:00.316 Runloop[19756:1191521] -[MyView drawRect:]2015-04-21 00:26:00.350 Runloop[19756:1191521] -[Empty viewDidAppear:]

Ignore the project name, because today I was thinking about runloop, alas ~ The world is unpredictable ~~~

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.