iOS Simple View的啟動流程,iossimple

來源:互聯網
上載者:User

iOS Simple View的啟動流程,iossimple
 建立工程

    選擇|File|New|Project,在快顯功能表中選擇Simple View Application,即可完成新項目的建立。t1

AppDelegate.h,

  • AppDelegate.m
  • ViewController.h
  • ViewController.m
  • Main.stroyboard
  • info.plist
  • main.m等檔案
  • 啟動流程

    app程式啟動過程如下:

     main函數
    #import <UIKit/UIKit.h> //UIKit是基於Cocoa Touch的架構,包含了UILable, UIButton等各個控制項元素#import "AppDelegate.h"  //申明了AppDelegate函數,用於完成App的初始化配置等int main(int argc, char * argv[]) {    @autoreleasepool {        NSLog(@"main");//調用AppDelegate
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));  
        }}

        和C代碼一樣,main為入口函數。在main函數中,匯入了<UIKit/UIKit.h>以及AppDelegate.h

    <UIKit/UIKit.h>為iOS主要架構,包含了各個主要控制項。

        main函數的參數與其他Linux的標準main函數一樣,int argc表示參數數量,char * argv[]為後面的參數,每個參數之間採用空格分開。

    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

        UIApplicationMain()方法根據我們提供的AppDelegate類名稱來建立UIApplication的一個執行個體,且將這個
    AppDelegate作為UIApplication的委託,一般我們可以通過類方法[UIApplication shareApplication]來擷取對UIApplication的一個引用。

        在UIApplication接收到系統事件和生命週期事件時,會把相應的事件傳遞給UIApplicationDelegate進行處理,下表所列的生命週期函數大都是可選的。

    AppDelegate

    AppDelegate為整個應用的一個代理,提供者啟動,退出等類似監控的介面。

    AppDelegate.h
    #import <UIKit/UIKit.h>//需要實現UIApplicationDelegate的方法@interface AppDelegate : UIResponder <UIApplicationDelegate>  //申明了UIWindow類,名稱為window的屬性,它是一個視窗,用來接收訊息和載入控制項@property (strong, nonatomic) UIWindow *window;  @end

    AppDelegate類中,繼承了UIResponder類,並需要實現UISpplicationDelegate協議。

    AppDelegate.m

    在UIApplication接收到系統事件和生命週期事件時,會把相應的事件傳遞給UIApplicationDelegate進行處理,

    #import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    NSLog(@"didFinishLaunchingWithOptions");    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.    NSLog(@"applicationWillResignActive");}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.    NSLog(@"applicationDidEnterBackground");}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.    NSLog(@"applicationWillEnterForeground");}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.    NSLog(@"applicationDidBecomeActive");}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.    NSLog(@"applicationWillTerminate");}@end

    具體見下面表格

    函數 說明
    - (void)applicationWillResignActive:(UIApplication *)application 當應用程式將要入非使用中執行,在此期間,應用程式不接收訊息或事件,比如來電話了
    - (void)applicationDidBecomeActive:(UIApplication *)application 當應用程式入活動狀態執行,這個剛好跟上面那個方法相反
    - (void)applicationDidEnterBackground:(UIApplication *)application 當程式被推送到背景時候調用。所以要設定後台繼續運行,則在這個函數裡面設定即可
    - (void)applicationWillEnterForeground:(UIApplication *)application 當程式從後台將要重新回到前台時候調用,這個剛好跟上面的那個方法相反。
    - (void)applicationWillTerminate:(UIApplication *)application 當程式將要退出是被調用,通常是用來儲存資料和一些退出前的清理工作。這個需要要設定UIApplicationExitsOnSuspend的索引值。
    - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application iPhone裝置只有有限的記憶體,如果為應用程式分配了太多記憶體作業系統會終止應用程式的運行,在終止前會執行這個方法,通常可以在這裡進行記憶體清理工作防止程式被終止
    - (void)applicationDidFinishLaunching:(UIApplication*)application 當程式載入後執行
    - (void)application:(UIApplication)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame 當StatusBar框將要變化時執行

    - (void)application:(UIApplication*)application willChangeStatusBarOrientation:
    (UIInterfaceOrientation)newStatusBarOrientation
    duration:(NSTimeInterval)duration

    當StatusBar框方向將要變化時執行
    - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url 通過url執行

     

        iPhone中的應用程式很容易受到打擾,比如一個來電可能導致應用程式失去焦點,如果這個時候接聽了電話,那麼應用程式會轉到後台運行。
    還有很多其它類似的事件會導致iPhone應用程式失去焦點,在應用程式失去焦點前會調用委託類的applicationWillResignActive()方法,
    而應用程式再次擷取到焦點的時候會調用applicationDidBecomeActive()方法。 

        比如在運行應用程式的時候鎖屏會調用委託類的applicationWillResignActive()方法,而當螢幕被解鎖的時候,又會調用applicationDidBecomeActive()方法。 

        UIApplication委託AppDelegate,則AppDelegate必須得實現UIApplicationDelegate協議,這個協議我們可以當成是java中的一個介面,
    協議中定義了一系列方法,我們必須在子類中將其實現,然後底層UIApplication會自動去調用我們已經定義好的方法,這有點類似java中Ioc方向控制機制。

    AppDelegate的應用例子頁面的載入和配置

    在didFinishLaunchingWithOptions方法中,主要完成頁面初始化時的準備工作,例如:建立各種視圖,讀取並設定設定檔等。

    讀取和配置的例子:

    NSUserDefaults *usrConfig= [NSUserDefaults standardUserDefaults];   //建立UserDefaults對象        //檢查是否之前app運行過    if ([ usrConfig boolForKey:@"hasRunBefore"] != YES)    {        // 設定個標記使此if中欄位只在第一次運行時執行        [ usrConfig setBool:YES forKey:@"hasRunBefore"];        [ usrConfig synchronize];// 把設定的索引值對同步

    建立視窗並設定背景

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    //初始化視窗self.window.backgroundColor = [UIColor whiteColor];  //設定背景色 [self.window makeKeyAndVisible];    //顯示視窗

    設定icon上的數字表徵圖

    //設定主介面icon上的數字表徵圖,在2.0中引進, 預設為0      [UIApplication sharedApplication].applicationIconBadgeNumber = 4;

    設定搖動手勢的時候,是否支援redo,undo操作

    //搖動手勢,是否支援redo undo操作。     //3.0以後引進,預設YES      [UIApplication sharedApplication].applicationSupportsShakeToEdit =YES;
    全域變數設定

    可以在AppDelegate.h中定義需要全域使用的變數

    @interface AppDelegate:UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSMutableArray*info;@property (strong, nonatomic) NSMutableArray *name;@end

    在AppDelegate.m的didFinishLaunchingWithOptions方法中,初始化這些屬性:

    self.info = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];self.name = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];

    在其他需要使用全域變數的方法中,擷取全域變數

    AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
    StoryBoard

    UIApplication 對象掃描 Info.plist 檔案,根據設定的storyboard名稱,載入storyboard.

    #import <UIKit/UIKit.h>@interface ViewController : UIViewController@end

    ViewController.m中,完成介面的最終配置和載入。

    #import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.     NSLog(@"viewDidLoad");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

    至此,就可以完成簡單的SimpleViewApplication的載入和配置工作。

    啟動打樣流程如下:

    當系統進入後台後, WillResignActive和DidEnterBackground被調用。

    《著作權,轉載請帶有作者名稱》

    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.