最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
iOS中AppDelegate的各函数的含义
时间:2022-06-25 23:44:14 编辑:袖梨 来源:一聚教程网
在大家刚接触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;这个函数里面,这样我也省下了不少时间,其他的函数的运用,大家可以敞开胸怀的去畅想了。
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22