iOS Development UI Chapter-Program Startup principle and UIApplicationFirst,uiapplication 1. Brief introduction
(1) uiapplication An object is a symbol of an application, and a UIApplication object represents an application.
(2) Each application has its own UIApplication object, and is a singleton, if you try to create a new UIApplication object in the program, then the error prompt.
(3) This singleton object can be obtained by [Uiapplicationsharedapplication]
(4) An iOS program is created after the first object is the UIApplication object, and only one (through the code to get two UIApplication objects, the print address can see the address is the same).
(5) Use the UIApplication object to perform some application-level operations
2. Application-level operation examples:
1) Set the application icon in the upper right corner of the red reminder number (such as QQ message, the icon will show a new message, such as three-way bar. )
@property (nonatomic) Nsinteger Applicationiconbadgenumber;
code Implementation and effects:
- (void) viewdidload{[Super Viewdidload]; //Create and add a buttonUIButton *btn=[[uibutton Alloc]initwithframe:cgrectmake ( -, -, -, -)]; [BTN Settitle:@"Button"Forstate:uicontrolstatenormal]; [Btn Setbackgroundcolor:[uicolor Browncolor]; [Btn addtarget:self Action: @selector (OnClick) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];}-(void) onclick{NSLog (@"button click event"); //error, there can be only one unique UIApplication object that can no longer be created//uiapplication *app=[[uiapplication Alloc]init]; //get the UIApplication object for the program by SharedapplicationUIApplication *app=[UIApplication sharedapplication]; App.applicationiconbadgenumber =123 ;}
2) Set the visibility of the networking indicator
@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;
code and Effects:
// set up an indicator's networked animation App.networkactivityindicatorvisible=yes;
3) Manage status bar
from iOS7 at the beginning, the system provides 2 How to manage the status bar
A. by Uiviewcontroller Management (Each of Uiviewcontroller can have their own different status bar).
In IOS7, by default, the status bar is managed by Uiviewcontroller, and Uiviewcontroller can easily manage the visibility and style of the status bar by implementing the following methods
The style of the status bar-(Uistatusbarstyle) Preferredstatusbarstyle;
Visibility of the status bar-(BOOL) Prefersstatusbarhidden;
#pragma mark-set the style of the status bar-(Uistatusbarstyle ) preferredstatusbarstyle{// set to White // return uistatusbarstylelightcontent; // default is black return Uistatusbarstyledefault;} #pragma mark-set whether the status bar is hidden (no)-(BOOL ) prefersstatusbarhidden{return NO;
B. by uiapplication Management (the status bar of an application is managed uniformly by it)
If you want to use uiapplication to manage the status bar, you first have to modify the Info.plist settings
Code:
//get the UIApplication object for the program by SharedapplicationUIApplication *app=[UIApplication sharedapplication]; App.applicationiconbadgenumber=123; //set up an indicator's networked animationapp.networkactivityindicatorvisible=YES; //set the style of the status bar//App.statusbarstyle=uistatusbarstyledefault;//Default (Black)//set to white + animated effect[app Setstatusbarstyle:uistatusbarstylelightcontent Animated:yes]; //set whether the status bar is hiddenapp.statusbarhidden=YES; //sets whether the status bar hides + animated effects[App Setstatusbarhidden:yes Withanimation:uistatusbaranimationfade];
C. Supplementary
Since both can be managed on the status bar, when should you use it? If the style of the status bar is set only once, use uiapplication to manage it, and if the status bar is hidden, the style is different then it is managed with the controller. UIApplication to manage has additional benefits that can provide animation effects.
4) OpenURL: Method
UIApplication has a very powerful OpenURL: method
-(BOOL) OpenURL: ( Nsurl *) URL;
OpenURL: some of the functions of the method are
Call UIApplication *app = [Uiapplicationsharedapplication]; [App openurl:[nsurlurlwithstring:@ "tel://10086"];
Send SMS [App openurl:[nsurlurlwithstring:@ "sms://10086"];
send e-mail [App openurl:[nsurlurlwithstring:@ "Mailto://[email protected]"];
open a Web resource [App openurl:[nsurlurlwithstring:@ "http://ios.itcast.cn"];
Open Other app program OpenURL method, you can open other apps.
URL Additions: URL: A Uniform Resource locator that is used to uniquely represent a resource. URL format: protocol header://Host address/resource Path network resources: Http/ftp and so on to represent the address of Baidu on a picture http://www.baidu.com/images/20140603/abc.png local resources: file:///users/ Apple/desktop/abc.png (host address omitted)Second,uiapplication Delegate
1. Brief description
all mobile operating systems have a fatal disadvantage: app It is very easy to be disturbed. For example, a call or lock screen will cause the app to go backstage or even be terminated.
There are a number of other similar situations that can cause the app to be disturbed, causing some system events when the app is disturbed, and UIApplication notifies its delegate object, allowing delegate agents to handle these system events.
Function: When interrupted, notifies the agent to enter the background.
each time a new project is completed, there is a "Appdelegate" the class of words, it is uiapplication the agent, Njappdelegate has complied with the Uiapplicationdelegate protocol by default and is already a proxy for uiapplication.
2. Proxy methods
1 #import "YYAppDelegate.h"2 3 @implementationyyappdelegate4 5 //called when the application is finished (automatically called by the system)6-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions7 {8NSLog (@"didfinishlaunchingwithoptions");9 returnYES;Ten } One A //called when it is about to lose its active state (loses focus, cannot interact) -- (void) Applicationwillresignactive: (UIApplication *) Application - { theNSLog (@"resignactive"); - } - - //regain focus (ability to interact with users) +- (void) Applicationdidbecomeactive: (UIApplication *) Application - { +NSLog (@"becomeactive"); A } at - //called when the application enters the background - //The application's data is typically stored in this method, as well as the state -- (void) Applicationdidenterbackground: (UIApplication *) Application - { -NSLog (@"Background"); in } - to //called when the application is about to enter the foreground + //typically restores the application's data in this method, as well as the state -- (void) Applicationwillenterforeground: (UIApplication *) Application the { *NSLog (@"Foreground"); $ }Panax Notoginseng - //This method is called when the application is about to be destroyed the //Note: This method cannot be called if the application is in a suspended state +- (void) Applicationwillterminate: (UIApplication *) Application A { the } + - //when the application receives a memory warning, it calls the $ //It is common to release unwanted memory in this method $- (void) Applicationdidreceivememorywarning: (UIApplication *) Application - { -NSLog (@"memorywarning"); the } - @endApplications typically have five statuses: Official document App.statesThree, the principle of program start
Uiapplicationmain
A uiapplicationmain function is executed in the main function.
Intuiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString *delegateclassname);
ARGC, argv: direct transfer to Uiapplicationmain for related processing can be
Principalclassname: Specifies the application class name (symbol of the app), which must be uiapplication (or subclass). If nil, use the UIApplication class as the default value
Delegateclassname: Specifies the application's proxy class, which must comply with the Uiapplicationdelegate protocol
The Uiapplicationmain function creates a UIApplication object based on Principalclassname and creates a delegate object from Delegateclassname. and assigns the delegate object to the delegate property in the UIApplication object
The application's main Runloop (the event loop) is then created, and the event is processed (the application:didfinishlaunchingwithoptions of the delegate object is called first after the program is completed: method)
The Uiapplicationmain function returns when the program exits normally
#import<UIKit/UIKit.h>#import "YYAppDelegate.h"intMainintargcChar*argv[]) {@autoreleasepool {//return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Yyappdelegate class])); //return Uiapplicationmain (argc, argv, @ "UIApplication", Nsstringfromclass ([Yyappdelegate class])); /*ARGC: The number of parameters that the system or user passed in argv: the actual parameter that the system or user passed in 1. Creates a UIApplication object based on the third parameter passed in 2. Creates a u based on the incoming fourth generation Proxy for the IApplication object 3. Set the proxy object that was just created for UIApplication 4. Turn on an event loop*/ returnUiapplicationmain (argc, argv,@"uiapplication",@"yyappdelegate"); }}
Code and parameter description for system entry:
ARGC: System or user-passed parameter argv: actual parameter passed in by the system or user 1. Based on the third parameter passed in, Create a UIApplication object 2. Based on the fourth incoming agent that generated the UIApplication object, 3. Set the proxy object that you just created as a proxy for uiapplication 4. Turn on an event loop (which can be understood as a dead loop inside) This time loop is a queue (advanced first The first to add in firsthow iOS programs start