您的位置:首页 > 移动开发

UIApplicationDelegateDemo--应用程序前台、后台分析

2012-03-14 08:53 477 查看
  建立UIApplicationDelegateDemo主要用来明确应用程序进入前台、按home键进入后台以及在此唤醒进入前台函数的调用顺序。

//
//  AppDelegate.h
//  UIApplicationDelegateDemo
//
//  Created by Fox on 12-3-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end


//
//  AppDelegate.m
//  UIApplicationDelegateDemo
//
//  Created by Fox on 12-3-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
/*
*当程序完成载入,并且可能有额外的启动选项时被调用,用于程序的初始化
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"Fox:didFinishLaunchingWithOptions");
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
/*
*应用程序初始化完成后,程序转为激活状态时被调用
*/
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"Fox:applicationDidBecomeActive");
/*
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.
*/
}

/*
*应用程序转为后台(非激活)状态时被调用,即按home键后先调用该方法
*/
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"Fox:applicationWillResignActive");
}

/*
*程序转到后台后,调用此方法,若想在后台运行程序,在此处添加.
*应用此方法来释放资源,保存数据,使计时器停止,存储应用程序状态和信息
*/
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Fox:applicationDidEnterBackground");
}

/*
*应用程序将要从后台进入前台时调用带方法
*/
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"Fox:applicationWillEnterForeground");
/*
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.
*/
}

/*
*应用程序将要终止时调用
*/
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"Fox:applicationWillTerminate");
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}

@end


程序运行后,调用的函数如下:



点击home键后:



在此点击程序,程序返回主界面:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: