建立UIApplicationDelegateDemo主要用来明确应用程序进入前台、按home键进入后台以及在此唤醒进入前台函数的调用顺序。
1 //
2 // AppDelegate.h
3 // UIApplicationDelegateDemo
4 //
5 // Created by Fox on 12-3-14.
6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @class ViewController;
12
13 @interface AppDelegate : UIResponder <UIApplicationDelegate>{
14
15 }
16
17 @property (strong, nonatomic) UIWindow *window;
18 @property (strong, nonatomic) ViewController *viewController;
19
20 @end
1 //
2 // AppDelegate.m
3 // UIApplicationDelegateDemo
4 //
5 // Created by Fox on 12-3-14.
6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "AppDelegate.h"
10
11 #import "ViewController.h"
12
13 @implementation AppDelegate
14
15 @synthesize window = _window;
16 @synthesize viewController = _viewController;
17
18 - (void)dealloc
19 {
20 [_window release];
21 [_viewController release];
22 [super dealloc];
23 }
24 /*
25 *当程序完成载入,并且可能有额外的启动选项时被调用,用于程序的初始化
26 */
27 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
28 {
29 NSLog(@"Fox:didFinishLaunchingWithOptions");
30 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
31 self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
32 self.window.rootViewController = self.viewController;
33 [self.window makeKeyAndVisible];
34 return YES;
35 }
36 /*
37 *应用程序初始化完成后,程序转为激活状态时被调用
38 */
39 - (void)applicationDidBecomeActive:(UIApplication *)application
40 {
41 NSLog(@"Fox:applicationDidBecomeActive");
42 /*
43 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.
44 */
45 }
46
47
48 /*
49 *应用程序转为后台(非激活)状态时被调用,即按home键后先调用该方法
50 */
51 - (void)applicationWillResignActive:(UIApplication *)application
52 {
53 NSLog(@"Fox:applicationWillResignActive");
54 }
55
56 /*
57 *程序转到后台后,调用此方法,若想在后台运行程序,在此处添加.
58 *应用此方法来释放资源,保存数据,使计时器停止,存储应用程序状态和信息
59 */
60 - (void)applicationDidEnterBackground:(UIApplication *)application
61 {
62 NSLog(@"Fox:applicationDidEnterBackground");
63 }
64
65
66 /*
67 *应用程序将要从后台进入前台时调用带方法
68 */
69 - (void)applicationWillEnterForeground:(UIApplication *)application
70 {
71 NSLog(@"Fox:applicationWillEnterForeground");
72 /*
73 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.
74 */
75 }
76
77
78 /*
79 *应用程序将要终止时调用
80 */
81 - (void)applicationWillTerminate:(UIApplication *)application
82 {
83 NSLog(@"Fox:applicationWillTerminate");
84 /*
85 Called when the application is about to terminate.
86 Save data if appropriate.
87 See also applicationDidEnterBackground:.
88 */
89 }
90
91 @end
程序运行后,调用的函数如下:
点击home键后:
在此点击程序,程序返回主界面: