欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中的應用啟動原理以及嵌套模型開發(fā)示例詳解

 更新時間:2015年12月22日 09:37:40   作者:文頂頂  
這篇文章主要介紹了iOS中的應用啟動原理以及嵌套模型開發(fā)示例詳解,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

程序啟動原理和UIApplication
 
一、UIApplication
1.簡單介紹
(1)UIApplication對象是應用程序的象征,一個UIApplication對象就代表一個應用程序。

(2)每一個應用都有自己的UIApplication對象,而且是單例的,如果試圖在程序中新建一個UIApplication對象,那么將報錯提示。

(3)通過[UIApplicationsharedApplication]可以獲得這個單例對象

(4) 一個iOS程序啟動后創(chuàng)建的第一個對象就是UIApplication對象,且只有一個(通過代碼獲取兩個UIApplication對象,打印地址可以看出地址是相同的)。

(5)利用UIApplication對象,能進行一些應用級別的操作

2.應用級別的操作示例:

1)設置應用程序圖標右上角的紅色提醒數(shù)字(如QQ消息的時候,圖標上面會顯示1,2,3條新信息等。)

復制代碼 代碼如下:

@property(nonatomic) NSInteger applicationIconBadgeNumber;

代碼實現(xiàn)和效果:
復制代碼 代碼如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //創(chuàng)建并添加一個按鈕
    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(@"按鈕點擊事件");
    //錯誤,只能有一個唯一的UIApplication對象,不能再進行創(chuàng)建
//    UIApplication *app=[[UIApplication alloc]init];
   
    //通過sharedApplication獲取該程序的UIApplication對象
    UIApplication *app=[UIApplication sharedApplication];
    app.applicationIconBadgeNumber=123;
}

2015122293127564.png (320×147)

2)設置聯(lián)網(wǎng)指示器的可見性

復制代碼 代碼如下:

@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;

代碼和效果:
復制代碼 代碼如下:

  //設置指示器的聯(lián)網(wǎng)動畫
    app.networkActivityIndicatorVisible=YES;

2015122293203933.png (320×159)

3)管理狀態(tài)欄

從iOS7開始,系統(tǒng)提供了2種管理狀態(tài)欄的方式

a.通過UIViewController管理(每一個UIViewController都可以擁有自己不同的狀態(tài)欄).

在iOS7中,默認情況下,狀態(tài)欄都是由UIViewController管理的,UIViewController實現(xiàn)下列方法就可以輕松管理狀態(tài)欄的可見性和樣式

狀態(tài)欄的樣式  

復制代碼 代碼如下:
  - (UIStatusBarStyle)preferredStatusBarStyle;

狀態(tài)欄的可見性 

復制代碼 代碼如下:
 -(BOOL)prefersStatusBarHidden;

復制代碼 代碼如下:

#pragma mark-設置狀態(tài)欄的樣式
-(UIStatusBarStyle)preferredStatusBarStyle
{
    //設置為白色
    //return UIStatusBarStyleLightContent;
    //默認為黑色
     return UIStatusBarStyleDefault;
}
#pragma mark-設置狀態(tài)欄是否隱藏(否)
-(BOOL)prefersStatusBarHidden
{
    return NO;
}

b.通過UIApplication管理(一個應用程序的狀態(tài)欄都由它統(tǒng)一管理)

如果想利用UIApplication來管理狀態(tài)欄,首先得修改Info.plist的設置
代碼:

2015122293242447.png (696×208)

代碼:

復制代碼 代碼如下:

//通過sharedApplication獲取該程序的UIApplication對象
    UIApplication *app=[UIApplication sharedApplication];
    app.applicationIconBadgeNumber=123;
   
    //設置指示器的聯(lián)網(wǎng)動畫
    app.networkActivityIndicatorVisible=YES;
    //設置狀態(tài)欄的樣式
    //app.statusBarStyle=UIStatusBarStyleDefault;//默認(黑色)
    //設置為白色+動畫效果
      [app setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    //設置狀態(tài)欄是否隱藏
    app.statusBarHidden=YES;
      //設置狀態(tài)欄是否隱藏+動畫效果
    [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

c.補充

既然兩種都可以對狀態(tài)欄進行管理,那么什么時候該用什么呢?
如果狀態(tài)欄的樣式只設置一次,那就用UIApplication來進行管理;
如果狀態(tài)欄是否隱藏,樣式不一樣那就用控制器進行管理。
UIApplication來進行管理有額外的好處,可以提供動畫效果。
 4)openURL:方法

UIApplication有個功能十分強大的openURL:方法

復制代碼 代碼如下:

- (BOOL)openURL:(NSURL*)url;

openURL:方法的部分功能有

打電話

復制代碼 代碼如下:
UIApplication *app = [UIApplicationsharedApplication]; [app openURL:[NSURLURLWithString:@"tel://10086"]];

發(fā)短信

復制代碼 代碼如下:
[app openURL:[NSURLURLWithString:@"sms://10086"]];

發(fā)郵件

復制代碼 代碼如下:
[app openURL:[NSURLURLWithString:@"mailto://12345@qq.com"]];

打開一個網(wǎng)頁資源

復制代碼 代碼如下:
[app openURL:[NSURLURLWithString:@"http://ios.itcast.cn"]];

打開其他app程序   openURL方法,可以打開其他APP。

 URL補充:
URL:統(tǒng)一資源定位符,用來唯一的表示一個資源。
URL格式:協(xié)議頭://主機地址/資源路徑
網(wǎng)絡資源:http/ ftp等   表示百度上一張圖片的地址   http://www.baidu.com/images/20140603/abc.png
本地資源:file:///users/apple/desktop/abc.png(主機地址省略)
 
二、UIApplication Delegate
1.簡單說明

所有的移動操作系統(tǒng)都有個致命的缺點:app很容易受到打擾。比如一個來電或者鎖屏會導致app進入后臺甚至被終止。

還有很多其它類似的情況會導致app受到干擾,在app受到干擾時,會產(chǎn)生一些系統(tǒng)事件,這時UIApplication會通知它的delegate對象,讓delegate代理來處理這些系統(tǒng)事件。

作用:當被打斷的時候,通知代理進入到后臺。

2015122293311566.png (687×377)

每次新建完項目,都有個帶有“AppDelegate”字眼的類,它就是UIApplication的代理,NJAppDelegate默認已經(jīng)遵守了UIApplicationDelegate協(xié)議,已經(jīng)是UIApplication的代理。

2015122293332799.png (902×378)

2.代理方法

復制代碼 代碼如下:

#import "YYAppDelegate.h"

@implementation YYAppDelegate

// 當應用程序啟動完畢的時候就會調(diào)用(系統(tǒng)自動調(diào)用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}

// 即將失去活動狀態(tài)的時候調(diào)用(失去焦點, 不可交互)
- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"ResignActive");
}

// 重新獲取焦點(能夠和用戶交互)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"BecomeActive");
}

// 應用程序進入后臺的時候調(diào)用
// 一般在該方法中保存應用程序的數(shù)據(jù), 以及狀態(tài)
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Background");
}

// 應用程序即將進入前臺的時候調(diào)用
// 一般在該方法中恢復應用程序的數(shù)據(jù),以及狀態(tài)
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"Foreground");
}

// 應用程序即將被銷毀的時候會調(diào)用該方法
// 注意:如果應用程序處于掛起狀態(tài)的時候無法調(diào)用該方法
- (void)applicationWillTerminate:(UIApplication *)application
{
}

// 應用程序接收到內(nèi)存警告的時候就會調(diào)用
// 一般在該方法中釋放掉不需要的內(nèi)存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"MemoryWarning");
}
@end


應用程序一般有五個狀態(tài):官方文檔app.states
 
三、程序啟動原理
UIApplicationMain

main函數(shù)中執(zhí)行了一個UIApplicationMain這個函數(shù)

intUIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

argc、argv:直接傳遞給UIApplicationMain進行相關處理即可

principalClassName:指定應用程序類名(app的象征),該類必須是UIApplication(或子類)。如果為nil,則用UIApplication類作為默認值

 delegateClassName:指定應用程序的代理類,該類必須遵守UIApplicationDelegate協(xié)議

UIApplicationMain函數(shù)會根據(jù)principalClassName創(chuàng)建UIApplication對象,根據(jù)delegateClassName創(chuàng)建一個delegate對象,并將該delegate對象賦值給UIApplication對象中的delegate屬性

接著會建立應用程序的Main Runloop(事件循環(huán)),進行事件的處理(首先會在程序完畢后調(diào)用delegate對象的application:didFinishLaunchingWithOptions:方法)

程序正常退出時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ù)個數(shù)
         argv: 系統(tǒng)或者用戶傳入的實際參數(shù)
         1.根據(jù)傳入的第三個參數(shù)創(chuàng)建UIApplication對象
         2.根據(jù)傳入的第四個產(chǎn)生創(chuàng)建UIApplication對象的代理
         3.設置剛剛創(chuàng)建出來的代理對象為UIApplication的代理
         4.開啟一個事件循環(huán)
         */
         return UIApplicationMain(argc, argv, @"UIApplication", @"YYAppDelegate");
    }
}


系統(tǒng)入口的代碼和參數(shù)說明:

argc:系統(tǒng)或者用戶傳入的參數(shù)
argv:系統(tǒng)或用戶傳入的實際參數(shù)
1.根據(jù)傳入的第三個參數(shù),創(chuàng)建UIApplication對象
2.根據(jù)傳入的第四個產(chǎn)生創(chuàng)建UIApplication對象的代理
3.設置剛剛創(chuàng)建出來的代理對象為UIApplication的代理
4.開啟一個事件循環(huán)(可以理解為里面是一個死循環(huán))這個時間循環(huán)是一個隊列(先進先出)先添加進去的先處理
 
ios程序啟動原理

2015122293442984.png (716×363)

四、程序啟動的完整過程

1.main函數(shù)

2.UIApplicationMain

* 創(chuàng)建UIApplication對象

* 創(chuàng)建UIApplication的delegate對象

 

3.delegate對象開始處理(監(jiān)聽)系統(tǒng)事件(沒有storyboard)

* 程序啟動完畢的時候, 就會調(diào)用代理的application:didFinishLaunchingWithOptions:方法

* 在application:didFinishLaunchingWithOptions:中創(chuàng)建UIWindow

* 創(chuàng)建和設置UIWindow的rootViewController

* 顯示窗口

 

3.根據(jù)Info.plist獲得最主要storyboard的文件名,加載最主要的storyboard(有storyboard)

* 創(chuàng)建UIWindow

* 創(chuàng)建和設置UIWindow的rootViewController

* 顯示窗口


使用嵌套模型完成的一個簡單汽車圖標展示程序
一、plist文件和項目結構圖

2015122293517495.png (833×587)

說明:這是一個嵌套模型的示例

二、代碼示例:

復制代碼 代碼如下:

 YYcarsgroup.h文件代碼:
//
//  YYcarsgroup.h
//  07-汽車展示(高級)
//
//  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-汽車展示(高級)
//
//  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]) {
        //嵌套的字典轉模型
        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];
        }
         // 賦值存儲模型的數(shù)組給屬性
        self.cars=arrayM;
    }
    return self;
}

+(instancetype)carsgroupWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
@end


YYcars.h文件
復制代碼 代碼如下:

//
//  YYcars.h
//  07-汽車展示(高級)
//
//  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-汽車展示(高級)
//
//  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-汽車展示(高級)
//
//  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- 實現(xiàn)懶加載
//1.從包中讀取數(shù)據(jù)
//2.字典轉模型
//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-  實現(xiàn)tableview的數(shù)據(jù)展示
//1.設置數(shù)據(jù)源,遵守協(xié)議
//2.返回組
//3.返回行
//4.每組每行對應的數(shù)據(jù)
//4.1去緩存中去取cell
//4.2若沒有,則創(chuàng)建cell,并蓋章
//4.3設置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設置cell的數(shù)據(jù)
    //設置對應的組
    YYcarsgroup *carsgroup=self.car[indexPath.section];
    //設置對應的行
    YYcars *yycars=carsgroup.cars[indexPath.row];

    cell.imageView.image=[UIImage imageNamed:yycars.icon];
    cell.textLabel.text=yycars.name;
    //4.4返回cell
    return cell;
}

//設置每組的標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    YYcarsgroup *carsgroup=self.car[section];
    return carsgroup.title;
}

//設置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //利用kvc取出所有的標題
    NSArray *title=[self.car  valueForKeyPath:@"title"];
    return title;
}

//隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return  YES;
}
@end


實現(xiàn)效果:

2015122293556403.png (320×495)

三、注意點

1.設置索引

代碼如下:

復制代碼 代碼如下:

//設置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //利用kvc取出所有的標題
    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];
    }

相關文章

最新評論