iPhone开发之生命周期(1)
在初次接触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.zzzyk.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
作者:js_dada
补充:移动开发 , IOS ,