在初次接觸iPhone開發的時候,簡直就是一頭霧水,這是哪裡跟哪裡啊?本是做Android開發的我,第一次建立iPhone開發而得到初始代碼,心裡那糾結比我初次接觸Android的情況還要糟糕!不說那麼多廢話了,第一編文章首先來熟悉一下iPhone程序的生命周期吧,這對整個開發周期也是非常有用的!
iPhone app 與 Android app在生命周期在大體上是相同,但是不同的時Android的程序生命周期都體現在每個頁面Activity上,也就是說,在開發Android app的時候,我們只需要管理好每個頁面Activity的生命周期,app的生命周期就無需我們太操心了!而iPhone app是嚴格按照MVC模型開發出來的程序,“委托”這一概念是非常重要的,理所當然,iPhone app的生命周期也是在“委托”上實現的,我們在建立iPhone app的初始狀態就可以看到兩個文件:AppDelegate.h和AppDelegate.m!這兩個文件就是我剛剛所說的“委托”的頭文件和實現文件!AppDelegate.m上就可以看到app的生命周期所觸發全部方法!iPhone的生命周期分為兩種,一是程序的生命周期,二是視圖(也就是相當於Android的Activity)的生命周期!
首先,介紹程序的生命周期:(表述能力有待提高,上代碼最實際)
[plain]
//首次加載,觸發的方法,開始加載程序
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"app didFinishLaunchingWithOptions");
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
//當用戶點擊菜單健,程序放棄激活狀態
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"applicationWillResignActive");
}
//進入後台,線程被掛起
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"applicationDidEnterBackground");
}
//程序未結束,點擊程序返回前台,再次獲取激活狀態,觸發applicationDidBecomeActive方法!
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"applicationWillEnterForeground");
}
//完成激活,可以與用戶交互
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"applicationDidBecomeActive");
}
//程序結束時觸發
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"applicationWillTerminate");
}
下面讓我們看一下視圖的生命周期:
[plain]
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
}
- (void)viewDidUnload
{
[super viewDidUnload];
secondViewController = nil;
NSLog(@"viewDidUnload");
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
NSLog(@"viewWillAppear");
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
NSLog(@"viewDidAppear");
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
NSLog(@"viewDidDisappear");
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
NSLog(@"viewWillDisappear");
}
從方法名,我們都可以看得出,視圖生命周期裡面大概觸發的方法。
現在我們運行一下程序,然後從第一個視圖切換到第二個視圖,再回到第一個視圖。讓我們看一下,從程序、視圖的出現,消亡所觸發的所有方法: www.2cto.com
[plain]
2012-05-10 11:29:13.426 iPhoneLifeCycle[877:f803] app didFinishLaunchingWithOptions
2012-05-10 11:29:13.459 iPhoneLifeCycle[877:f803] viewDidLoad
2012-05-10 11:29:13.462 iPhoneLifeCycle[877:f803] viewWillAppear
2012-05-10 11:29:13.465 iPhoneLifeCycle[877:f803] applicationDidBecomeActive
2012-05-10 11:29:13.472 iPhoneLifeCycle[877:f803] viewDidAppear
2012-05-10 11:29:15.054 iPhoneLifeCycle[877:f803] viewWillDisappear
2012-05-10 11:29:15.061 iPhoneLifeCycle[877:f803] viewDidDisappear
2012-05-10 11:29:16.476 iPhoneLifeCycle[877:f803] viewDidLoad
2012-05-10 11:29:16.478 iPhoneLifeCycle[877:f803] viewWillAppear
2012-05-10 11:29:16.483 iPhoneLifeCycle[877:f803] viewDidAppear
2012-05-10 11:29:21.790 iPhoneLifeCycle[877:f803] applicationWillResignActive
2012-05-10 11:29:21.796 iPhoneLifeCycle[877:f803] applicationDidEnterBackground