For investors
股價(jià):
5.36 美元 %For investors
股價(jià):
5.36 美元 %認(rèn)真做教育 專心促就業(yè)
在初次接觸iPhone開發(fā)的時(shí)候,簡(jiǎn)直就是一頭霧水,這是哪里跟哪里啊?本是做Android開發(fā)的我,第一次建立iPhone開發(fā)而得到初始代碼,心里那糾結(jié)比我初次接觸Android的情況還要糟糕!不說(shuō)那么多廢話了,第一編文章首先來(lái)熟悉一下iPhone程序的生命周期吧,這對(duì)整個(gè)開發(fā)周期也是非常有用的!
iPhone app 與 Android app在生命周期在大體上是相同,但是不同的時(shí)Android的程序生命周期都體現(xiàn)在每個(gè)頁(yè)面Activity上,也就是說(shuō),在開發(fā)Android app的時(shí)候,我們只需要管理好每個(gè)頁(yè)面Activity的生命周期,app的生命周期就無(wú)需我們太操心了!而iPhone app是嚴(yán)格按照MVC模型開發(fā)出來(lái)的程序,“委托”這一概念是非常重要的,理所當(dāng)然,iPhone app的生命周期也是在“委托”上實(shí)現(xiàn)的,我們?cè)诮Phone app的初始狀態(tài)就可以看到兩個(gè)文件:AppDelegate.h和AppDelegate.m!這兩個(gè)文件就是我剛剛所說(shuō)的“委托”的頭文件和實(shí)現(xiàn)文件!AppDelegate.m上就可以看到app的生命周期所觸發(fā)全部方法!iPhone的生命周期分為兩種,一是程序的生命周期,二是視圖(也就是相當(dāng)于Android的Activity)的生命周期!
首先,介紹程序的生命周期:(表述能力有待提高,上代碼最實(shí)際)
[plain]
//首次加載,觸發(fā)的方法,開始加載程序
- (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;
}
//當(dāng)用戶點(diǎn)擊菜單健,程序放棄激活狀態(tài)
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"applicationWillResignActive");
}
//進(jìn)入后臺(tái),線程被掛起
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"applicationDidEnterBackground");
}
//程序未結(jié)束,點(diǎn)擊程序返回前臺(tái),再次獲取激活狀態(tài),觸發(fā)applicationDidBecomeActive方法!
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"applicationWillEnterForeground");
}
//完成激活,可以與用戶交互
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"applicationDidBecomeActive");
}
//程序結(jié)束時(shí)觸發(fā)
- (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");
}
從方法名,我們都可以看得出,視圖生命周期里面大概觸發(fā)的方法。
現(xiàn)在我們運(yùn)行一下程序,然后從第一個(gè)視圖切換到第二個(gè)視圖,再回到第一個(gè)視圖。讓我們看一下,從程序、視圖的出現(xiàn),消亡所觸發(fā)的所有方法: #
[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
【免責(zé)聲明】本文部分系轉(zhuǎn)載,轉(zhuǎn)載目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé)。如涉及作品內(nèi)容、版權(quán)和其它問(wèn)題,請(qǐng)?jiān)?0日內(nèi)與聯(lián)系我們,我們會(huì)予以更改或刪除相關(guān)文章,以保證您的權(quán)益!