天天看點

UIApplicationDelegate分析小結

我們開發出來的運作在iOS平台上的應用程式都有一個UIApplication類的對象。

1、是iOS應用程式的起始點,并負責初始化和顯示UIWindow;

2、負責加載應用程式的第一個UIView到UIWindow中;

3、幫助管理應用程式的生命周期;

4、接收事件,再轉給它的委托"UIApplicationDelegate"來處理;此委托可處理的事件包括:應用程式的生命周期事件如程式啟動和關閉、系統事件如來電和記事項警告;

(miki西遊 @mikixiyou 原文連結: http://mikixiyou.iteye.com/blog/1742950 )

雖然UIApplication負責接收事件,但它無需我們來修改。而它的負責處理事件的委托類,其遵循UIApplicationDelegate協定,是需要我們進行開發。

例如

應用BirdsApp的委托類的聲明如下:

@interface BirdsAppDelegate : UIResponder <UIApplicationDelegate>

這個類需要實作UIApplicationDelegate協定中的方法,用于處理UIApplication接收的事件。

這些方法有很多,大概是這些:

1、應用完成登入的事件處理方法;

2、應用中斷的事件處理方法;

3、記憶體很低的事件處理方法;

4、重要改變發生的事件處理方法;

在開發時,我們需實作的最重要的方法是application:didFinishLaunchingWithOptions:,其他方法也應該去實作,雖然它們都是可選的。

在XCode4.5版中,如果使用它的模闆建立項目,Xcode将會為我們建立遵守UIApplicationDelegate協定的方法,這些方法的實作代碼需要我們自己去開發。

例如,建立一個名稱為BirdsApp的項目,XCode會自動建立BirdsAppDelegate.h和BirdsAppDelegate.m檔案。

/*BirdsAppDelegate 應用委托類*/

#import <UIKit/UIKit.h>
@class BirdSightingDataController;
@class BirdsMasterViewController;
@interface BirdsAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BirdSightingDataController *dataController;
@property (strong, nonatomic) BirdsMasterViewController *firstViewController;
@end

#import "BirdsAppDelegate.h"
#import "BirdSightingDataController.h"
#import "BirdsMasterViewController.h"
@implementation BirdsAppDelegate
@synthesize window = _window, dataController = _dataController, firstViewController= _firstViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
BirdsMasterViewController *firstViewController = (BirdsMasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
firstViewController.dataController = aDataController;
self.dataController = aDataController;

return YES;
}
@end      

建立在storyboard基礎上的應用程式,作為UIApplication類的對象,在啟動過程中将進行下列操作。

首先,加載info.plist檔案内容到一個NSDictionary對象中,讀取字典對象的鍵UIMainStoryboardFile對應的值得到storyboard配置檔案名稱,通常檔案名稱會是MainStoryboard.storyboard。

其次,加載MainStoryboard.storyboard檔案,根據檔案中記錄的第一個視圖控制器的值,自動執行個體化該控制器。該控制器稱為主視圖控制器。

再次,将主視圖控制器的所有的視圖使用addSubView方法加載到UIWindow對象中去。

這個時間點,算是完成應用啟動,調用應用委托類中的application:didFinishLaunchingWithOptions:的方法。

而以前的應用程式用Interface Builder開發,使用nib管理視圖,那麼必須在應用委托類中實作加載主視圖控制器的所有視圖和執行個體化UIWindow對象的操作。

#import <UIKit/UIKit.h>

@interface GuessChildAppDelegate : NSObject <UIApplicationDelegate> {
  UIWindow *window;
    UINavigationController* simple_;
}

@property (nonatomic, retain)  UIWindow *window;

@end


#import "GuessChildAppDelegate.h"
#import "WifeBirthdayController.h"

@implementation GuessChildAppDelegate

@synthesize window;


#pragma mark -
#pragma mark Application lifecycle

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    //CGRect frame = [[UIScreen mainScreen] bounds];
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
    WifeBirthdayController* rootViewController = [[[WifeBirthdayController alloc] init] autorelease];
    simple_ = [[UINavigationController alloc] initWithRootViewController:rootViewController];
   
    [window addSubview:simple_.view];

    // Override point for customization after application launch.
   
  [window makeKeyAndVisible];
   
    return YES;
}

-(void)dealloc {
    [window release];
    [super dealloc];
}

@end      

本想記錄下應用委托類的用法,結果複習了一下應用程式的啟動過程。

附一份社會熱點評論短文。

《史記. 切糕傳》: 販切糕者, 多為西域人也。富可敵國。其業暴利,因有民曰:「一兩五十,一刀三斤。」更有甚者曰:「一刀楊幂上床, 兩刀帝都買房,三刀蘭博到手,四刀蓋茨認娘」。 故歐美人遇奢侈且無力購買之物時大多驚呼:「哦,賣糕的!」