一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

iOS中AppDelegate的各函数的含义

时间:2015-11-01 编辑:简简单单 来源:一聚教程网


在大家刚接触iOS的时候,每次生成一个项目的时候,系统都会自动生成一个AppDelegate的类,其中各种函数,对初学者的我来说,真是一点摸不着头脑,也不知道如何运用,今天就给大家来讲讲AppDelegate的一些用法和含义。

来看看AppDelegate.m文件中的一些函数的含义吧。


//
//  AppDelegate.m
//
//
//  Created by Kenshin Cui on 14-2-23.
//  Copyright (c) 2014年 Kenshin Cui. All rights reserved.
//
 
#import "AppDelegate.h"
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}
 
- (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.
}
 
- (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.
}
 
- (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.
}
 
- (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.
}
 
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
 
@end
其实说白了,这个类中定义了应用程序生命周期中各个事件的执行方法:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;程序启动之后执行,只有在第一次程序启动后才执行,以后不再执行;

– (void)applicationWillResignActive:(UIApplication *)application;程序将要被激活时执行,程序激活用户才能操作;

– (void)applicationDidEnterBackground:(UIApplication *)application;程序进入后台后执行,注意进入后台时会先失去焦点再进入后台;

– (void)applicationWillEnterForeground:(UIApplication *)application;程序将要进入前台时执行;

– (void)applicationDidBecomeActive:(UIApplication *)application;程序被激活后执行,注意程序被激活时会先进入前台再被激活;

– (void)applicationWillTerminate:(UIApplication *)application;程序在终止时执行,包括正常终止或异常终止,例如说一个应用程序在后台程序占用太多内存这时会意外终止调用此方法;

这里所有函数的含义也就搞清楚了,我习惯是把一些整个程序都用到的一些属性或函数,直接写在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;这个函数里面,这样我也省下了不少时间,其他的函数的运用,大家可以敞开胸怀的去畅想了。

热门栏目