Program Startup principle and UIApplication
First, UIApplication
1. Brief introduction
(1) The UIApplication object is a symbol of the application, and a UIApplication object represents an application.
(2) Each application has its own UIApplication object, and is a single example, if you try to create a new UIApplication object in the program, then the error prompts.
(3) This single instance object can be obtained through [uiapplicationsharedapplication]
(4) An iOS program started after the creation of the first object is the UIApplication object, and only one (through the code to get two UIApplication objects, print the address can see that the address is the same).
(5) using the UIApplication object, can carry on some application level operation
2. Examples of application-level operations:
1 set the application icon in the upper right corner of the red reminder number (such as QQ message, the icon will display 1,2,3 new information, etc.). )
Copy Code code as follows:
@property (nonatomic) Nsinteger applicationiconbadgenumber;
Code implementation and Effects:
Copy Code code as follows:
-(void) viewdidload
{
[Super Viewdidload];
Create and add a button
UIButton *btn=[[uibutton alloc]initwithframe:cgrectmake (100, 100, 60, 30)];
[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, you can only have a unique uiapplication object that can no longer be created
UIApplication *app=[[uiapplication Alloc]init];
Get the program's UIApplication object by sharedapplication
UIApplication *app=[uiapplication Sharedapplication];
app.applicationiconbadgenumber=123;
}
2 Set the visibility of the network indicator
Copy Code code as follows:
@property (nonatomic,getter=isnetworkactivityindicatorvisible) BOOL networkactivityindicatorvisible;
Code and Effects:
Copy Code code as follows:
Set up an indicator's networked animation
App.networkactivityindicatorvisible=yes;
3) Management status bar
Starting with IOS7, the system provides 2 ways to manage the status bar
A. Through Uiviewcontroller management (each uiviewcontroller can have its own different status bar).
In IOS7, the status bar is managed by Uiviewcontroller by default, and Uiviewcontroller can easily manage the visibility and style of the status bar by implementing the following methods
Style of status bar
Copy Code code as follows:
-(Uistatusbarstyle) Preferredstatusbarstyle;
Visibility of the status bar
Copy Code code as follows:
-(BOOL) Prefersstatusbarhidden;
Copy Code code as follows:
#pragma mark-to set the style of the status bar
-(Uistatusbarstyle) Preferredstatusbarstyle
{
Set to White
return uistatusbarstylelightcontent;
Default is Black
return uistatusbarstyledefault;
}
#pragma mark-to set whether the status bar is hidden (no)
-(BOOL) Prefersstatusbarhidden
{
return NO;
}
B. Through 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:
Code:
Copy Code code as follows:
Get the program's UIApplication object by sharedapplication
UIApplication *app=[uiapplication Sharedapplication];
app.applicationiconbadgenumber=123;
Set up an indicator's networked animation
App.networkactivityindicatorvisible=yes;
Set the style of the status bar
app.statusbarstyle=uistatusbarstyledefault;//Default (Black)
Set to White + animation effect
[App Setstatusbarstyle:uistatusbarstylelightcontent Animated:yes];
To set whether the status bar is hidden
App.statusbarhidden=yes;
Set whether the status bar hides + animation effects
[App Setstatusbarhidden:yes Withanimation:uistatusbaranimationfade];
C. supplementing
Since both can be managed on the status bar, then what should be used?
If the style of the status bar is set only once, use uiapplication to manage it;
If the status bar is hidden and the style is different, manage it with a controller.
UIApplication to manage there are additional benefits that can provide an animation effect.
4) OpenURL: Method
UIApplication has a very powerful OpenURL: method
Copy Code code as follows:
-(BOOL) OpenURL: (nsurl*) URL;
OpenURL: Some of the features of the method are
Call
Copy Code code as follows:
UIApplication *app = [Uiapplicationsharedapplication]; [App openurl:[nsurlurlwithstring:@ "tel://10086"]];
Texting
Copy Code code as follows:
[App openurl:[nsurlurlwithstring:@ "sms://10086"]];
Send an email
Copy Code code as follows:
[App openurl:[nsurlurlwithstring:@ "mailto://12345@qq.com"]];
Open a Web page resource
Copy Code code as follows:
[App openurl:[nsurlurlwithstring:@ "http://ios.itcast.cn"]];
Open Other app OpenURL method, you can open other app.
URL Supplement:
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 The address of a picture on Baidu Http://www.baidu.com/images/20140603/abc.png
Local resources: File:///users/apple/desktop/abc.png (host address omitted)
Second, uiapplication Delegate
1. Simple description
All mobile operating systems have a fatal drawback: apps are easily disturbed. For example, a call or a lock screen can cause the app to go backstage or even be terminated.
There are many other similar situations that can cause the app to be disturbed, and when the app is disturbed, it generates some system events, and UIApplication notifies its delegate object and lets the delegate agent handle the system events.
Role: When interrupted, notify the agent to enter the background.
Every time you create a new project, there is a class with the word "appdelegate", it is uiapplication agent, Njappdelegate has complied with the Uiapplicationdelegate agreement by default, is already the agent of UIApplication.
2. Proxy method
Copy Code code as follows:
#import "YYAppDelegate.h"
@implementation Yyappdelegate
Called when the application is started (automatically called by the system)
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
NSLog (@ "didfinishlaunchingwithoptions");
return YES;
}
Call (lose focus, not interact) when you are about to lose your active state
-(void) Applicationwillresignactive: (uiapplication *) application
{
NSLog (@ "resignactive");
}
Regain focus (ability to interact with users)
-(void) Applicationdidbecomeactive: (uiapplication *) application
{
NSLog (@ "becomeactive");
}
Call when the application enters the background
Typically, the application's data is saved in this method, and the status
-(void) Applicationdidenterbackground: (uiapplication *) application
{
NSLog (@ "Background");
}
Call when the application is about to enter the foreground
Typically restores the application's data in this method, and the status
-(void) Applicationwillenterforeground: (uiapplication *) application
{
NSLog (@ "foreground");
}
This method is called when the application is about to be destroyed
Note: This method cannot be invoked if the application is in a suspended state
-(void) Applicationwillterminate: (uiapplication *) application
{
}
When the application receives a memory warning, it calls the
Typically frees unwanted memory in this method
-(void) applicationdidreceivememorywarning: (uiapplication *) application
{
NSLog (@ "memorywarning");
}
@end
Applications typically have five states: official documentation App.states
third, the principle of program initiation
Uiapplicationmain
A uiapplicationmain function is executed in the main function
Intuiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString *delegateclassname);
ARGC, argv: Direct transmission to Uiapplicationmain for related processing can be
Principalclassname: Specifies the application class name (the icon for the app), which must be a uiapplication (or subclass). If nil, the UIApplication class is used as the default value
Delegateclassname: Specifies the proxy class for the application, which must comply with the Uiapplicationdelegate protocol
The Uiapplicationmain function creates a UIApplication object based on the Principalclassname, and creates a delegate object based on the Delegateclassname. and assigns the delegate object to the delegate property in the UIApplication object
The application's main Runloop (event loop) is then created, and the event is processed (the application:didfinishlaunchingwithoptions of the delegate object is invoked first after the program finishes: method)
The Uiapplicationmain function returns when the program exits normally
Copy Code code as follows:
#import <UIKit/UIKit.h>
#import "YYAppDelegate.h"
int main (int argc, char * argv[])
{
@autoreleasepool {
Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Yyappdelegate class]);
Return Uiapplicationmain (argc, argv, @ "UIApplication", Nsstringfromclass ([Yyappdelegate class]);
/*
ARGC: Number of parameters that the system or user passes in
ARGV: Actual parameters passed in by system or user
1. Create a UIApplication object based on the third parameter passed in
2. The agent that creates the UIApplication object according to the incoming fourth generation
3. Set the proxy object that you just created as UIApplication
4. Open an event loop
*/
Return Uiapplicationmain (argc, argv, @ "uiapplication", @ "Yyappdelegate");
}
}
Code and parameter descriptions for the system entry:
ARGC: System or user passed in parameters
ARGV: Actual parameters passed in by system or user
1. Create a UIApplication object based on the third parameter passed in
2. The agent that creates the UIApplication object according to the incoming fourth generation
3. Set the proxy object that you just created as UIApplication
4. Open an event loop (which can be understood to be a dead loop inside) This time cycle is a queue (FIFO) first added to the first processing
iOS program startup principle
The complete process of program initiation
1.main function
2.UIApplicationMain
* Create UIApplication Object
* Create a UIApplication delegate object
3.delegate object starts processing (listening) system events (no storyboard)
* When the program is started, the agent's application:didfinishlaunchingwithoptions is invoked: method
* Create UIWindow in application:didfinishlaunchingwithoptions:
* Create and set the UIWindow Rootviewcontroller
* Display window
3. According to Info.plist obtain the most main storyboard file name, load the most important storyboard (have storyboard)
* Create UIWindow
* Create and set the UIWindow Rootviewcontroller
* Display window
a simple car icon display program completed using nested models
I. plist documents and Project structure chart
Description: This is an example of a nested model
Second, the code example:
Copy Code code as follows:
YYcarsgroup.h File Code:
//
YYcarsgroup.h
07-Car Show (Advanced)
//
Created by Apple on 14-5-28.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Yycarsgroup:nsobject
@property (nonatomic,copy) NSString *title;
@property (Nonatomic,strong) Nsarray *cars;
-(Instancetype) Initwithdict: (Nsdictionary *) dict;
+ (Instancetype) carsgroupwithdict: (Nsdictionary *) dict;
@end
YYCARSGROUP.M File Code:
Copy Code code as follows:
//
yycarsgroup.m
07-Car Show (Advanced)
//
Created by Apple on 14-5-28.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYcarsgroup.h"
#import "YYcars.h"
@implementation Yycarsgroup
-(Instancetype) Initwithdict: (Nsdictionary *) dict
{
if (Self=[super init]) {
Nested dictionary-turn model
self.title=dict[@ "title"];
Attention
Nsarray *dictcars=dict[@ "Cars"];
Write as follows to improve performance
Nsmutablearray *arraym=[nsmutablearray ArrayWithCapacity:dictcars.count];
For (Nsdictionary *dict in Dictcars) {
Yycars *yycars=[[yycars Alloc]initwithdict:dict];
[Arraym Addobject:yycars];
}
An array of assignment storage models gives properties
Self.cars=arraym;
}
return self;
}
+ (Instancetype) carsgroupwithdict: (Nsdictionary *) dict
{
return [[Self alloc]initwithdict:dict];
}
@end
YYcars.h file
Copy Code code as follows:
//
YYcars.h
07-Car Show (Advanced)
//
Created by Apple on 14-5-28.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Yycars:nsobject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
-(Instancetype) Initwithdict: (Nsdictionary *) dict;
+ (Instancetype) carswithdict: (Nsdictionary *) dict;
@end
YYCARS.M file
Copy Code code as follows:
//
Yycars.m
07-Car Show (Advanced)
//
Created by Apple on 14-5-28.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYcars.h"
@implementation Yycars
-(Instancetype) Initwithdict: (Nsdictionary *) dict
{
if (Self=[super init]) {
self.name=dict[@ "Name"];
self.icon=dict[@ "icon"];
}
return self;
}
+ (Instancetype) carswithdict: (Nsdictionary *) dict
{
return [[Self alloc]initwithdict:dict];
}
@end
YYVIEWCONTROLLER.M file
Copy Code code as follows:
//
Yyviewcontroller.m
07-Car Show (Advanced)
//
Created by Apple on 14-5-28.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYcarsgroup.h"
#import "YYcars.h"
@interface Yyviewcontroller () <UITableViewDataSource>
@property (Strong, nonatomic) Iboutlet UITableView *tableview;
@property (Nonatomic,strong) Nsarray *car;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
SELF.TABLEVIEW.ROWHEIGHT=60.F;
self.tableview.datasource=self;
NSLog (@ "%d", self.car.count);
}
#pragma mark-to implement lazy loading
1. Reading data from the package
2. Dictionary Turn model
3. Return to Cars
-(Nsarray *) car
{
if (_car==nil) {
NSString *fullpath= [[NSBundle mainbundle]pathforresource:@ "Cars_total.plist" oftype:nil];
Nsarray *arraym=[nsarray Arraywithcontentsoffile:fullpath];
Nsmutablearray *carsarray=[nsmutablearray Array];
For (Nsdictionary *dict in Arraym) {
Yycarsgroup *carsgroup=[yycarsgroup Carsgroupwithdict:dict];
[Carsarray Addobject:carsgroup];
}
_car=[carsarray copy];
}
return _car;
}
#pragma mark-to realize tableview data display
1. Set up the data source and observe the agreement
2. Return group
3. Return rows
4. Each set of data corresponding to each line
4.1 Go to the cache to fetch the cell
4.2 If not, create a cell and seal it
4.3 Setting the cell data
4.4 Return cell
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return self.car.count;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
Yycarsgroup *carsgroup=self.car[section];
return carsgroup.cars.count;
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
Static NSString *identifier=@ "car";
4.1 Go to the cache to fetch the cell
UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
4.2 If not, create a cell and seal it
if (Cell==nil) {
Cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier];
}
4.3 Setting the cell data
Set up the corresponding group
Yycarsgroup *carsgroup=self.car[indexpath.section];
Set the corresponding row
Yycars *yycars=carsgroup.cars[indexpath.row];
Cell.imageview.image=[uiimage ImageNamed:yycars.icon];
Cell.textlabel.text=yycars.name;
4.4 Return cell
return cell;
}
Set the title of each group
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
{
Yycarsgroup *carsgroup=self.car[section];
return carsgroup.title;
}
Set Index
-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView
{
Use KVC to remove all headings
Nsarray *title=[self.car valueforkeypath:@ "title"];
return title;
}
Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
@end
Implementation effect:
Third, the attention point
1. Set Index
The code is as follows:
Copy Code code as follows:
Set Index
-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView
{
Use KVC to remove all headings
Nsarray *title=[self.car valueforkeypath:@ "title"];
return title;
}
2.cell Performance Optimization
The code is as follows:
Copy Code code as follows:
Static NSString *identifier=@ "car";
4.1 Go to the cache to fetch the cell
UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
4.2 If not, create a cell and seal it
if (Cell==nil) {
Cell=[[uitableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier];
}