Detailed Uikit Framework: it defines the iOS main thread execution process

Source: Internet
Author: User
Tags uikit

If we use a template to create a new project, such as Master Detail application or tabbed application, Then Xcode will automatically generate the Main.storyboard file for us, so that the thread execution processes that were defined in the middle of the source code are now all defined in the storyboard file. With the story version of course there is the benefit of the story version, the only visual elements that can be defined now allow programmers to define visually, but for programmers who are not very familiar with iOS programming, this way of building the UI is not conducive to maintenance changes, because programmers are not easy to see the actual execution of threads.

In code, the programmer can see when the thread creates an instance of UIWindow, defines the interface elements to be hosted by the window, and when to draw a graph from the code and data in the UIWindow instance. Method makekeyandvisible tells how the thread should draw.

Main thread Execution Flow

iOS app main thread execution flow for example, a block represents a collection of code or other resources that a thread needs to obtain, and the arrow direction represents the direction in which the thread is transferred.

is the declaration period of the main thread. Let's look at the main function:

#import <UIKit/UIKit.h>#import"QSAppDelegate.h"int Main (intChar * argv[]) {    @autoreleasepool {        returnclass ]));    }}

This is the entry for the thread, after which the function uiapplicationmain is executed.

Uiapplicationmain ()

The function is the core of the initialization program, which accepts 4 parameters.

int uiapplicationmain (int argc, char *argv[], nsstring *principalclassname, nsstring *delegateclassname );

ARGC and argv:

Two parameters received from main ();

A third parameter:

The name of the main class (principal class name) must be the name of the UIApplication or its subclasses, an instance of this class that references the current iphone app and defines the code for the event loop. It tells the thread to read the Info.plist file for configuration information, and Main.storyboard, and if this parameter is nil, the default is @ "UIApplication";

The fourth parameter:

The name of the proxy class (delegate class name), the thread creates an instance of the proxy class in memory, and defines some code that handles the operating system-related events.

UIApplication class (defined by Uikit)

The object of UIApplication (or its subclasses) mainly does the following things:
1) is responsible for handling incoming user events and distributing event messages to the target object (sender, action) that listens to the message.
2) Manage and control views, including rendering, control behavior, current display view, and more.
3) The object corresponds to an application delegate object that is notified by the application when important events (which can include system events or life cycle control events) occur during some life cycles. For example, application startup, insufficient memory, or application completion, and so on, when these events occur, the application delegates to respond.

This class defines a lot of useful code that can be executed by the thread.

1. Set the number icon on the icon
Set the digital icon on the main interface icon, introduced in 2.0, the default is 0
[UIApplication sharedapplication].applicationiconbadgenumber = 4;
2. Whether the Redo,undo action is supported when setting the shake gesture

Shake gesture, whether the redo undo operation is supported. 3.0 after introduction, default Yes

[UIApplication Sharedapplication].applicationsupportsshaketoedit =yes;
3. Judging the state of the program operation
Determine the state of the program's operation and introduce it after 2.0
if ([uiapplication sharedapplication].applicationstate ==uiapplicationstateinactive) {
NSLog (@ "program in Operation State");
}
4. Prevent the screen from darkening into hibernation
Prevent screen from darkening, use cautiously, default to No 2.0
[UIApplication sharedapplication].idletimerdisabled =yes;
Use this feature sparingly, because it consumes very much power.
5. Show Networking Status
Show Networking Tags 2.0
[UIApplication sharedapplication].networkactivityindicatorvisible =yes;


6. Show an address on the map
nsstring* addresstext [email protected] "1 Infinite Loop, Cupertino, CA 95014";
URL encode the spaces
Addresstext = [Addresstext stringbyaddingpercentescapesusingencoding:nsasciistringencoding];
nsstring* Urltext = [NSString stringwithformat:@ "http://maps.google.com/maps?q=%@", Addresstext];

[[UIApplication Sharedapplication]openurl:[nsurl Urlwithstring:urltext]];
7. Send an email
NSString *recipients [email protected] "Mailto:[email protected][email protected],[email Protected]&subject=hello From california! ";
NSString *body [email protected] "&body=it is raining in sunny california!";
NSString *email = [NSString stringwithformat:@ "%@%@", recipients, body];
email = [email stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

[[UIApplication Sharedapplication]openurl:[nsurl Urlwithstring:email]];
8. Call a number
Call Google 411
[[UIApplication sharedapplication]openurl:[nsurl urlwithstring:@ "tel://8004664411"];
9. Send SMS
Text to Google SMS
[[UIApplication sharedapplication]openurl:[nsurl urlwithstring:@ "sms://466453"];
10. Open a URL
Lanuch any IPhone developers fav site
[[UIApplication sharedapplication]openurl:[nsurl urlwithstring:@ "http://itunesconnect.apple.com"];

Appdelegate class (defined by programmer)

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
When an application is opened due to other methods (such as URL designation or connection), the notification delegate is started
-(void) Applicationwillterminate: (uiapplication *) application
Notifies the delegate that the application will exit at shutdown, please do some cleanup work.
-(void) applicationdidreceivememorywarning: (uiapplication *) application
Notifies the delegate that the application received a low memory warning from the system. -(void) Applicationsignificanttimechange: (uiapplication *) application
Notification delegate system time changes (mainly refers to the time attribute, not the specific time value)
Open URL
-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL
Opens the specified URL
Control status bar Orientation change
–application:willchangestatusbarorientation:duration:
Device Orientation will change
–application:didchangestatusbarorientation:
Active status change
-(void) Applicationwillresignactive: (uiapplication *) application
Notifies the delegate that the application will go into an inactive state during which the application does not receive messages or events. -(void) Applicationdidbecomeactive: (uiapplication *) application
Notifies the delegate application to enter the active state, please restore the data

You can see the UIApplication header file implementation
@interface Uiapplication:uiresponder <uiactionsheetdelegate>{
@package
Id<uiapplicationdelegate> _delegate; This is the application delegate.
Nstimer .....
}
So, if we want to get appdelegate instances, we don't have to go through the import class, just:
uiapplicationdelegate* delegate = [[UIApplication sharedapplication] delegate];

Replace the story version with code

Say a lot about the real process of iOS program execution, let's examine our understanding.

If your engineering class starts with a TC.
1.import your appdelegate class, and modify the fourth parameter as follows:
Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Tcappdelegate class]));
2. Delete the Main.storyboard file
3. Delete the main storyboard file base name in the project's info.plist files

4. Modify the method defined in the delegate class:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; Uiviewcontroller*webpageviewcontroller =[[Webpageviewcontroller alloc] init]; Uiviewcontroller*personalviewcontroller =[[Personalviewcontroller alloc]init]; Uiviewcontroller*meviewcontroller =[[Meviewcontroller alloc]init]; Uinavigationcontroller*personalnavigationcontroller =[[Uinavigationcontroller alloc]init]; Personalnavigationcontroller.viewcontrollers=[Nsarray Arraywithobjects:personalviewcontroller, Nil]; Uinavigationcontroller*menavigationcontroller =[[Uinavigationcontroller alloc]init]; Menavigationcontroller.viewcontrollers=[Nsarray Arraywithobjects:meviewcontroller, Nil]; _tabbarcontroller=[[Uitabbarcontroller alloc]init]; _tabbarcontroller.viewcontrollers=[Nsarray Arraywithobjects:webpageviewcontroller, Personalnavigationcontroller, Menavigationcontroller,nil]; Self.window.rootViewController=_tabbarcontroller; _tabbarcontroller.selectedindex=0; Self.window.backgroundColor=[Uicolor Whitecolor];    [Self customizeappearance];    [Self.window makekeyandvisible]; returnYES;}

This avoids the loading of the window and the interface elements it hosts based on the story-version file's information.

Detailed Uikit Framework: it defines the iOS main thread execution process

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.