今天又学习了一下ios的有管知识,发现AppDelegate.m很重要,自己对这个文件一点都不懂,必须要学习一下啊。
delegate,委托的意思。
首先 AppDelegate.m AppDelegate.h是整个程序的入口,整个工程只有一个这样的的文件。
上网查找原来这个文件与应用的生命周期密切相关,应用杂不同的生命周期阶段会调用不同的方法,这和Android的生命周期是一样的,但是Android的生命周期是体现子啊Activity中的。我们先看一下应用的生命周期吧。参考:http://blog.youkuaiyun.com/totogo2010/article/details/8048652
相信大家都能看懂。
去官网看一下代码,分析一下代码中的方法与生命周期的那些阶段相对应:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
{ |
// Override point for customization after application launch. |
return YES; |
} 当应用程序启动后,进行的定制点。 我翻译的很菜,就是在这里定制你的UI。 |
|
- (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. |
} |
当应用从active状态进入到Inactive状态是,回调这个方法。例如电话,或者一条短信进来了或者用户退出应用,它将被放到后台。 使用此方法暂停正在进行的任务,使计时器暂停,降低OGL ES的速度???,游戏应该使用这个方法暂停游戏。 |
- (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. |
} |
看名字就知道,当进入到后台状态是调用这个方法。 使用这个方法去释放共享资源,保存用户数据,让计时器失效,存储足够的现在的应用状态信息以防止它不久被结束。 当你的应用支持后台运行的时候,这个方法被调用以代替applicationWillTerinate: 当用户退出时??? |
- (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. |
} |
从后台到inactive 状态是被调用,在这里可以撤销(之前的)进入后台时的改变。 |
- (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. |
} |
重新启动在应用进入inactive装态时被暂停的任务(或者至今还没有开启的任务) 。如果,应用之前就是在后台状态,此方法就会随意地刷新用户界面 |
- (void)applicationWillTerminate:(UIApplication *)application |
{ |
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. |
} |
当应用将要被终止的时候回调。适当地保存数据,参考applicationDidEnterBackground方法。 |