[轉]UIApplicationDelegate分析小結

來源:互聯網
上載者:User

標籤:android   class   code   http   com   strong   

轉載地址:http://www.apkbus.com/android-131646-1-1.html

我們開發出來的運行在iOS平台上的應用程式都有一個UIApplication類的對象。
1、是iOS應用程式的起始點,並負責初始化和顯示UIWindow;
2、負責載入應用程式的第一個UIView到UIWindow中;
3、協助管理應用程式的生命週期;
4、接收事件,再轉給它的委託"UIApplicationDelegate"來處理;此委託可處理的事件包括:應用程式的生命週期事件如程式啟動和關閉、系統事件如來電和記事項警告;
雖然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檔案。
  1. /*BirdsAppDelegate 應用委託類*/
  2. #import <UIKit/UIKit.h>
  3. [url=home.php?mod=space&uid=661]@class[/url] BirdSightingDataController;
  4. @class BirdsMasterViewController;
  5. @interface BirdsAppDelegate : UIResponder <UIApplicationDelegate>
  6. @property (strong, nonatomic) UIWindow *window;
  7. @property (strong, nonatomic) BirdSightingDataController *dataController;
  8. @property (strong, nonatomic) BirdsMasterViewController *firstViewController;
  9. @end
  10. #import "BirdsAppDelegate.h"
  11. #import "BirdSightingDataController.h"
  12. #import "BirdsMasterViewController.h"
  13. @implementation BirdsAppDelegate
  14. @synthesize window = _window, dataController = _dataController, firstViewController= _firstViewController;
  15. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  16. {
  17. UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
  18. BirdsMasterViewController *firstViewController = (BirdsMasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
  19. BirdSightingDataController *aDataController = [[BirdSightingDataController alloc] init];
  20. firstViewController.dataController = aDataController;
  21. self.dataController = aDataController;
  22. return YES;
  23. }
  24. @end
複製代碼建立在storyboard基礎上的應用程式,作為UIApplication類的對象,在啟動過程中將進行下列操作。

 

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

 

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

 

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



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




而以前的應用程式用Interface Builder開發,使用nib管理檢視,那麼必須在應用委託類中實現載入主視圖控制器的所有視圖和執行個體化UIWindow對象的操作。
  1. #import <UIKit/UIKit.h>
  2. @interface GuessChildAppDelegate : NSObject <UIApplicationDelegate> {
  3. UIWindow *window;
  4. UINavigationController* simple_;
  5. }
  6. @property (nonatomic, retain) UIWindow *window;
  7. @end
  8. #import "GuessChildAppDelegate.h"
  9. #import "WifeBirthdayController.h"
  10. @implementation GuessChildAppDelegate
  11. @synthesize window;
  12. #pragma mark -
  13. #pragma mark Application lifecycle
  14. -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  15. //CGRect frame = [[UIScreen mainScreen] bounds];
  16. window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  17. WifeBirthdayController* rootViewController = [[[WifeBirthdayController alloc] init] autorelease];
  18. simple_ = [[UINavigationController alloc] initWithRootViewController:rootViewController];
  19. [window addSubview:simple_.view];
  20. // Override point for customization after application launch.
  21. [window makeKeyAndVisible];
  22. return YES;
  23. }
  24. -(void)dealloc {
  25. [window release];
  26. [super dealloc];
  27. }
  28. @end
複製代碼本想記錄下應用委託類的用法,結果複習了一下應用程式的啟動過程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.