iOS中的應(yīng)用啟動(dòng)原理以及嵌套模型開發(fā)示例詳解
程序啟動(dòng)原理和UIApplication
一、UIApplication
1.簡(jiǎn)單介紹
(1)UIApplication對(duì)象是應(yīng)用程序的象征,一個(gè)UIApplication對(duì)象就代表一個(gè)應(yīng)用程序。
(2)每一個(gè)應(yīng)用都有自己的UIApplication對(duì)象,而且是單例的,如果試圖在程序中新建一個(gè)UIApplication對(duì)象,那么將報(bào)錯(cuò)提示。
(3)通過[UIApplicationsharedApplication]可以獲得這個(gè)單例對(duì)象
(4) 一個(gè)iOS程序啟動(dòng)后創(chuàng)建的第一個(gè)對(duì)象就是UIApplication對(duì)象,且只有一個(gè)(通過代碼獲取兩個(gè)UIApplication對(duì)象,打印地址可以看出地址是相同的)。
(5)利用UIApplication對(duì)象,能進(jìn)行一些應(yīng)用級(jí)別的操作
2.應(yīng)用級(jí)別的操作示例:
1)設(shè)置應(yīng)用程序圖標(biāo)右上角的紅色提醒數(shù)字(如QQ消息的時(shí)候,圖標(biāo)上面會(huì)顯示1,2,3條新信息等。)
@property(nonatomic) NSInteger applicationIconBadgeNumber;
代碼實(shí)現(xiàn)和效果:
- (void)viewDidLoad
{
[super viewDidLoad];
//創(chuàng)建并添加一個(gè)按鈕
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 60, 30)];
[btn setTitle:@"按鈕" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor brownColor]];
[btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)onClick
{
NSLog(@"按鈕點(diǎn)擊事件");
//錯(cuò)誤,只能有一個(gè)唯一的UIApplication對(duì)象,不能再進(jìn)行創(chuàng)建
// UIApplication *app=[[UIApplication alloc]init];
//通過sharedApplication獲取該程序的UIApplication對(duì)象
UIApplication *app=[UIApplication sharedApplication];
app.applicationIconBadgeNumber=123;
}
2)設(shè)置聯(lián)網(wǎng)指示器的可見性
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
代碼和效果:
//設(shè)置指示器的聯(lián)網(wǎng)動(dòng)畫
app.networkActivityIndicatorVisible=YES;
3)管理狀態(tài)欄
從iOS7開始,系統(tǒng)提供了2種管理狀態(tài)欄的方式
a.通過UIViewController管理(每一個(gè)UIViewController都可以擁有自己不同的狀態(tài)欄).
在iOS7中,默認(rèn)情況下,狀態(tài)欄都是由UIViewController管理的,UIViewController實(shí)現(xiàn)下列方法就可以輕松管理狀態(tài)欄的可見性和樣式
狀態(tài)欄的樣式
狀態(tài)欄的可見性
#pragma mark-設(shè)置狀態(tài)欄的樣式
-(UIStatusBarStyle)preferredStatusBarStyle
{
//設(shè)置為白色
//return UIStatusBarStyleLightContent;
//默認(rèn)為黑色
return UIStatusBarStyleDefault;
}
#pragma mark-設(shè)置狀態(tài)欄是否隱藏(否)
-(BOOL)prefersStatusBarHidden
{
return NO;
}
b.通過UIApplication管理(一個(gè)應(yīng)用程序的狀態(tài)欄都由它統(tǒng)一管理)
如果想利用UIApplication來管理狀態(tài)欄,首先得修改Info.plist的設(shè)置
代碼:
代碼:
//通過sharedApplication獲取該程序的UIApplication對(duì)象
UIApplication *app=[UIApplication sharedApplication];
app.applicationIconBadgeNumber=123;
//設(shè)置指示器的聯(lián)網(wǎng)動(dòng)畫
app.networkActivityIndicatorVisible=YES;
//設(shè)置狀態(tài)欄的樣式
//app.statusBarStyle=UIStatusBarStyleDefault;//默認(rèn)(黑色)
//設(shè)置為白色+動(dòng)畫效果
[app setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
//設(shè)置狀態(tài)欄是否隱藏
app.statusBarHidden=YES;
//設(shè)置狀態(tài)欄是否隱藏+動(dòng)畫效果
[app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
c.補(bǔ)充
既然兩種都可以對(duì)狀態(tài)欄進(jìn)行管理,那么什么時(shí)候該用什么呢?
如果狀態(tài)欄的樣式只設(shè)置一次,那就用UIApplication來進(jìn)行管理;
如果狀態(tài)欄是否隱藏,樣式不一樣那就用控制器進(jìn)行管理。
UIApplication來進(jìn)行管理有額外的好處,可以提供動(dòng)畫效果。
4)openURL:方法
UIApplication有個(gè)功能十分強(qiáng)大的openURL:方法
- (BOOL)openURL:(NSURL*)url;
openURL:方法的部分功能有
打電話
發(fā)短信
發(fā)郵件
打開一個(gè)網(wǎng)頁資源
打開其他app程序 openURL方法,可以打開其他APP。
URL補(bǔ)充:
URL:統(tǒng)一資源定位符,用來唯一的表示一個(gè)資源。
URL格式:協(xié)議頭://主機(jī)地址/資源路徑
網(wǎng)絡(luò)資源:http/ ftp等 表示百度上一張圖片的地址 http://www.baidu.com/images/20140603/abc.png
本地資源:file:///users/apple/desktop/abc.png(主機(jī)地址省略)
二、UIApplication Delegate
1.簡(jiǎn)單說明
所有的移動(dòng)操作系統(tǒng)都有個(gè)致命的缺點(diǎn):app很容易受到打擾。比如一個(gè)來電或者鎖屏?xí)?dǎo)致app進(jìn)入后臺(tái)甚至被終止。
還有很多其它類似的情況會(huì)導(dǎo)致app受到干擾,在app受到干擾時(shí),會(huì)產(chǎn)生一些系統(tǒng)事件,這時(shí)UIApplication會(huì)通知它的delegate對(duì)象,讓delegate代理來處理這些系統(tǒng)事件。
作用:當(dāng)被打斷的時(shí)候,通知代理進(jìn)入到后臺(tái)。
每次新建完項(xiàng)目,都有個(gè)帶有“AppDelegate”字眼的類,它就是UIApplication的代理,NJAppDelegate默認(rèn)已經(jīng)遵守了UIApplicationDelegate協(xié)議,已經(jīng)是UIApplication的代理。
2.代理方法
#import "YYAppDelegate.h"
@implementation YYAppDelegate
// 當(dāng)應(yīng)用程序啟動(dòng)完畢的時(shí)候就會(huì)調(diào)用(系統(tǒng)自動(dòng)調(diào)用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
// 即將失去活動(dòng)狀態(tài)的時(shí)候調(diào)用(失去焦點(diǎn), 不可交互)
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"ResignActive");
}
// 重新獲取焦點(diǎn)(能夠和用戶交互)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"BecomeActive");
}
// 應(yīng)用程序進(jìn)入后臺(tái)的時(shí)候調(diào)用
// 一般在該方法中保存應(yīng)用程序的數(shù)據(jù), 以及狀態(tài)
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Background");
}
// 應(yīng)用程序即將進(jìn)入前臺(tái)的時(shí)候調(diào)用
// 一般在該方法中恢復(fù)應(yīng)用程序的數(shù)據(jù),以及狀態(tài)
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"Foreground");
}
// 應(yīng)用程序即將被銷毀的時(shí)候會(huì)調(diào)用該方法
// 注意:如果應(yīng)用程序處于掛起狀態(tài)的時(shí)候無法調(diào)用該方法
- (void)applicationWillTerminate:(UIApplication *)application
{
}
// 應(yīng)用程序接收到內(nèi)存警告的時(shí)候就會(huì)調(diào)用
// 一般在該方法中釋放掉不需要的內(nèi)存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"MemoryWarning");
}
@end
應(yīng)用程序一般有五個(gè)狀態(tài):官方文檔app.states
三、程序啟動(dòng)原理
UIApplicationMain
main函數(shù)中執(zhí)行了一個(gè)UIApplicationMain這個(gè)函數(shù)
intUIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
argc、argv:直接傳遞給UIApplicationMain進(jìn)行相關(guān)處理即可
principalClassName:指定應(yīng)用程序類名(app的象征),該類必須是UIApplication(或子類)。如果為nil,則用UIApplication類作為默認(rèn)值
delegateClassName:指定應(yīng)用程序的代理類,該類必須遵守UIApplicationDelegate協(xié)議
UIApplicationMain函數(shù)會(huì)根據(jù)principalClassName創(chuàng)建UIApplication對(duì)象,根據(jù)delegateClassName創(chuàng)建一個(gè)delegate對(duì)象,并將該delegate對(duì)象賦值給UIApplication對(duì)象中的delegate屬性
接著會(huì)建立應(yīng)用程序的Main Runloop(事件循環(huán)),進(jìn)行事件的處理(首先會(huì)在程序完畢后調(diào)用delegate對(duì)象的application:didFinishLaunchingWithOptions:方法)
程序正常退出時(shí)UIApplicationMain函數(shù)才返回
#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: 系統(tǒng)或者用戶傳入的參數(shù)個(gè)數(shù)
argv: 系統(tǒng)或者用戶傳入的實(shí)際參數(shù)
1.根據(jù)傳入的第三個(gè)參數(shù)創(chuàng)建UIApplication對(duì)象
2.根據(jù)傳入的第四個(gè)產(chǎn)生創(chuàng)建UIApplication對(duì)象的代理
3.設(shè)置剛剛創(chuàng)建出來的代理對(duì)象為UIApplication的代理
4.開啟一個(gè)事件循環(huán)
*/
return UIApplicationMain(argc, argv, @"UIApplication", @"YYAppDelegate");
}
}
系統(tǒng)入口的代碼和參數(shù)說明:
argc:系統(tǒng)或者用戶傳入的參數(shù)
argv:系統(tǒng)或用戶傳入的實(shí)際參數(shù)
1.根據(jù)傳入的第三個(gè)參數(shù),創(chuàng)建UIApplication對(duì)象
2.根據(jù)傳入的第四個(gè)產(chǎn)生創(chuàng)建UIApplication對(duì)象的代理
3.設(shè)置剛剛創(chuàng)建出來的代理對(duì)象為UIApplication的代理
4.開啟一個(gè)事件循環(huán)(可以理解為里面是一個(gè)死循環(huán))這個(gè)時(shí)間循環(huán)是一個(gè)隊(duì)列(先進(jìn)先出)先添加進(jìn)去的先處理
ios程序啟動(dòng)原理
四、程序啟動(dòng)的完整過程
1.main函數(shù)
2.UIApplicationMain
* 創(chuàng)建UIApplication對(duì)象
* 創(chuàng)建UIApplication的delegate對(duì)象
3.delegate對(duì)象開始處理(監(jiān)聽)系統(tǒng)事件(沒有storyboard)
* 程序啟動(dòng)完畢的時(shí)候, 就會(huì)調(diào)用代理的application:didFinishLaunchingWithOptions:方法
* 在application:didFinishLaunchingWithOptions:中創(chuàng)建UIWindow
* 創(chuàng)建和設(shè)置UIWindow的rootViewController
* 顯示窗口
3.根據(jù)Info.plist獲得最主要storyboard的文件名,加載最主要的storyboard(有storyboard)
* 創(chuàng)建UIWindow
* 創(chuàng)建和設(shè)置UIWindow的rootViewController
* 顯示窗口
使用嵌套模型完成的一個(gè)簡(jiǎn)單汽車圖標(biāo)展示程序
一、plist文件和項(xiàng)目結(jié)構(gòu)圖
說明:這是一個(gè)嵌套模型的示例
二、代碼示例:
YYcarsgroup.h文件代碼:
//
// YYcarsgroup.h
// 07-汽車展示(高級(jí))
//
// 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文件代碼:
//
// YYcarsgroup.m
// 07-汽車展示(高級(jí))
//
// 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]) {
//嵌套的字典轉(zhuǎn)模型
self.title=dict[@"title"];
//注意
NSArray *dictcars=dict[@"cars"];
//像下面這樣寫可以提高性能
NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:dictcars.count];
for (NSDictionary *dict in dictcars) {
YYcars *yycars=[[YYcars alloc]initWithDict:dict];
[arrayM addObject:yycars];
}
// 賦值存儲(chǔ)模型的數(shù)組給屬性
self.cars=arrayM;
}
return self;
}
+(instancetype)carsgroupWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
@end
YYcars.h文件
//
// YYcars.h
// 07-汽車展示(高級(jí))
//
// 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文件
//
// YYcars.m
// 07-汽車展示(高級(jí))
//
// 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文件
//
// YYViewController.m
// 07-汽車展示(高級(jí))
//
// 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
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.rowHeight=60.f;
self.tableview.dataSource=self;
NSLog(@"%d",self.car.count);
}
#pragma mark- 實(shí)現(xiàn)懶加載
//1.從包中讀取數(shù)據(jù)
//2.字典轉(zhuǎn)模型
//3.返回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- 實(shí)現(xiàn)tableview的數(shù)據(jù)展示
//1.設(shè)置數(shù)據(jù)源,遵守協(xié)議
//2.返回組
//3.返回行
//4.每組每行對(duì)應(yīng)的數(shù)據(jù)
//4.1去緩存中去取cell
//4.2若沒有,則創(chuàng)建cell,并蓋章
//4.3設(shè)置cell的數(shù)據(jù)
//4.4返回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去緩存中去取cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//4.2若沒有,則創(chuàng)建cell,并蓋章
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//4.3設(shè)置cell的數(shù)據(jù)
//設(shè)置對(duì)應(yīng)的組
YYcarsgroup *carsgroup=self.car[indexPath.section];
//設(shè)置對(duì)應(yīng)的行
YYcars *yycars=carsgroup.cars[indexPath.row];
cell.imageView.image=[UIImage imageNamed:yycars.icon];
cell.textLabel.text=yycars.name;
//4.4返回cell
return cell;
}
//設(shè)置每組的標(biāo)題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
YYcarsgroup *carsgroup=self.car[section];
return carsgroup.title;
}
//設(shè)置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//利用kvc取出所有的標(biāo)題
NSArray *title=[self.car valueForKeyPath:@"title"];
return title;
}
//隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
實(shí)現(xiàn)效果:
三、注意點(diǎn)
1.設(shè)置索引
代碼如下:
//設(shè)置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//利用kvc取出所有的標(biāo)題
NSArray *title=[self.car valueForKeyPath:@"title"];
return title;
}
2.cell的性能優(yōu)化
代碼如下:
static NSString *identifier=@"car";
//4.1去緩存中去取cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//4.2若沒有,則創(chuàng)建cell,并蓋章
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
相關(guān)文章
iOS應(yīng)用中使用Auto Layout實(shí)現(xiàn)自定義cell及拖動(dòng)回彈
這篇文章主要介紹了iOS應(yīng)用中使用Auto Layout實(shí)現(xiàn)自定義cell及拖動(dòng)回彈的方法,自定義UITableViewCell并使用Auto Layout對(duì)其進(jìn)行約束可以方便地針對(duì)多尺寸屏幕進(jìn)行調(diào)整,代碼為Swift語言,需要的朋友可以參考下2016-03-03iOS無障礙適配西瓜視頻Voice?Over實(shí)踐示例
本文從研發(fā)的視角出發(fā),講述了如何使用?Voice?Over、如何適配?Voice?Over?以及適配過程中如果遇到問題應(yīng)該如何解決。希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05解決iOS11刷新tableview會(huì)出現(xiàn)漂移的現(xiàn)象
這篇文章主要介紹了解決iOS11刷新tableview會(huì)出現(xiàn)漂移的現(xiàn)象,需要的朋友可以參考下2017-10-10iOS開發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解
這篇文章主要介紹了iOS開發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解,代碼基礎(chǔ)傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01iOS漸變圓環(huán)旋轉(zhuǎn)動(dòng)畫CAShapeLayer CAGradientLayer
這篇文章主要介紹了iOS漸變圓環(huán)旋轉(zhuǎn)動(dòng)畫CAShapeLayer CAGradientLayer的相關(guān)資料,需要的朋友可以參考下2016-09-09簡(jiǎn)介iOS開發(fā)中應(yīng)用SQLite的模糊查詢和常用函數(shù)
這篇文章主要介紹了iOS開發(fā)中應(yīng)用SQLite的模糊查詢和常用函數(shù),SQLite是一個(gè)可作嵌入式的數(shù)據(jù)庫非常適合小型應(yīng)用使用,需要的朋友可以參考下2015-12-12