20150629_UI之应用程序生命周期及UI开源学习网址

http://code4app.com学习UI网址

创建第一个IOS应用程序的时候,我们一般创建一个IOS的single view application,程序的入口仍然是main函数,在main函数里调用了UIApplicationMain(argc,
argv, nil, NSStringFromClass([AppDelegate class]));这个方法会去调用我们的代理类,使代理类去执行我们的代码

//
//  main.m
//  IOS150629_UI_应用程序生命周期
//
//  Created by PengJunlong on 15/6/29.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
            //第三个参数nil:代表默认传入UIApplication类,或者传入该类的子类
            //第四个参数:传递应用程序的代理类

//        int UIApplicationMain (
//                               int argc,
//                               char *argv[],
//                               NSString *principalClassName,
//                               NSString *delegateClassName
//                               );
//              Description:
//        This function is called in the main entry point to create the application object and
//        the application delegate and set up the event cycle.
//        This function instantiates the application object from the principal class and instantiates
//        the delegate (if any) from the given class and sets the delegate for the application. It also
//        sets up the main event loop, including the application’s run loop, and begins processing events.
//        If the application’s Info.plist file specifies a main nib file to be loaded, by including the
//        NSMainNibFile key and a valid nib file name for the value, this function loads that nib file.
//            Despite the declared return type, this function never returns. For more information on how
//        this function behaves, see “Core App Objects” in App Programming Guide for iOS.

//             Parameters:
//        argc:The count of arguments in argv; this usually is the corresponding parameter to main.
//        argv:A variable list of arguments; this usually is the corresponding parameter to main.
//        principalClassName:The name of the UIApplication class or subclass. If you specify nil, UIApplication is assumed.
//        delegateClassName:The name of the class from which the application delegate is instantiated.
//        If principalClassName designates a subclass of UIApplication, you may designate the subclass
//        as the delegate; the subclass instance receives the application-delegate messages. Specify nil if you load
//        the delegate object from your application’s main nib file.

//            Returns
//            Even though an integer return type is specified, this function never returns. When users exits an iOS application by pressing the Home button, the application moves to the background.
    }
}

IOS程序的执行图:图片来自网络

AppDelegate是我们系统生成的一个代理类:
//
//  AppDelegate.h
//  IOS150629_UI_应用程序生命周期
//
//  Created by PengJunlong on 15/6/29.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
//
//  AppDelegate.m
//  IOS150629_UI_应用程序生命周期
//
//  Created by PengJunlong on 15/6/29.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "AppDelegate.h"

//开发工具:
//1.Xcode:编辑工程代码,修改工程
//2.IOS Simulator:模拟器,在Mac电脑上模拟iPhone设备的运行环境
//3.instrument:内存分析工具
//4.iPhone开发工具包(SDK):苹果官方提供的开发环境
//5.interface Builder(nib/xib):提供UI界面与用户的接口
//代码创建

//沙盒(sandBox)机制:
//出于安全考虑,每一个应用程序都有自己独立的文件目录结构
//应用程序之间不能共享数据
//iOS的应用只能访问为该应用创建的区域,不可访问其他区域,应用的其他非代码文件都存在此目录下,包括图片,属性文件plist,bundle,nib文件等,这块区域称之为沙盒(sandBox)。
//1.每个应用都有属于自己的存储空间,即沙盒
//2.应用只能访问自己的沙盒,不可访问其他区域
//3.如果应用需要进行文件操作,则必须将文件存放在沙盒中,尤其是数据库文件,在电脑上操作时,可以去访问,但是如果要装在真机上可以使用,必须将数据库文件拷贝至沙盒中。

//应用程序的状态:
//Not running:未运行,程序没启动
//Inactive   :未激活,程序在前台运行,不过没有接收到事件.在没有事件处理情况下程序通常停留在这个状态
//Active     :激活,程序在前台运行而且接收到了事件.这也是前台的一个正常模式
//Background :后台,程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会.时间到了以后会进入挂起状态(Suspended).有的程序经过特殊的请求后可以长期处于Background状态
//Suspended  :挂起,程序在后台不能执行代码.系统会自动把程序变成这个状态而且不会发出通知.当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存

@interface AppDelegate ()

@end

@implementation AppDelegate

//应用程序启动调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //应用程序启动后会调用这个方法
    //代码通常就写在该方法中
    NSLog(@"沙盒路径:%@",NSHomeDirectory());
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = nil;

    //获取当前应用程序对象
    UIApplication *app = [UIApplication sharedApplication];

    //获取当前应用程序的代理
    AppDelegate *delegate = [app delegate];
    NSLog(@"application = %@,app = %@,delegate = %@",application,app,delegate);
    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.

    //何时调用该方法(applicationWillResignActive:);
    //1.外部中断事件进入,如:电话或者短消息进入
    //2.从前台切换到后太台,及退出应用程序

    //作用:
    //1.暂停正在进行的任务,暂停定时器,降低OpenGL帧率,暂停正在运行的游戏
    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

//应用程序进入后台时调用
- (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.

    //释放共享资源,保存用户数据,销毁定时器,存储足够的应用程序的状态信息以使应用程序从后台恢复到前台
    //如果应用程序支持后台执行,该方法替代applicationWillTerminate:

    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

//应用程序进入前台时调用
- (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.

    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

//应用程序进入前台后会调用
- (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.

    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

//当内存告急时调用该方法,释放一些内存资源
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    //释放内存
    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

//应用不支持后台运行模式时,会调用该方法时应用程序终止
//在info.plist文件中添加key:Application does not run in background    value:YES   可以设置应用程序不支持后台运行
//通常我们是支持后台
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"%@ has been called.",NSStringFromSelector(_cmd));
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 00:15:53

20150629_UI之应用程序生命周期及UI开源学习网址的相关文章

ASP.NET管线与应用程序生命周期

ASP.NET管线与应用程序生命周期 ASP.NET管线与应用程序生命周期 8.1节介绍了IIS的系统架构和HTTP请求处理的总体流程,从中可以知道每个ASP.NET网站都对应着一个Web应用程序,此Web应用程序可以响应HTTP请求,为用户提供所需的信息.那么,ASP.NET应用程序具体是如何响应HTTP请求的?包括哪些具体的处理流程?这涉及到ASP.NET应用程序的生命周期问题. 8.2.1 ASP.NET应用程序生命周期* 本节以IIS 6为例分步介绍ASP.NET应用程序处理HTTP请求

IIS 7.0 的 ASP.NET 应用程序生命周期概述(转载)

IIS 7.0 的 ASP.NET 应用程序生命周期概述更新:2007 年 11 月本主题介绍在 IIS 7.0 集成模式下运行以及与 IIS 7.0 或更高版本一起运行的 ASP.NET 应用程序的应用程序生命周期.IIS 7.0 还支持经典模式,其行为类似于在 IIS 6.0 中运行的 ASP.NET.有关更多信息,请参见 IIS 5.0 和 6.0 的 ASP.NET 应用程序生命周期概述.IIS 7.0 集成管道是一种统一的请求处理管道,它同时支持本机代码和托管代码模块.实现 IHttp

asp.net应用程序生命周期和asp.net网页的生命周期

一.asp.net应用程序生命周期 asp.net应用程序生命周期以浏览器向web服务器(比如IIS服务器)发送请求为起点,先后经历web服务器下的ISAPI(Internet Server Application Programming Interface)扩展接收到应用程序的第一个请求并创建一个应用程序域,为每个请求创建ASP.NET核心对象(如HttpContext.HttpRequest.HttpResponse),将HttpApplication对象分配给请求(即通过创建HttpApp

asp.net应用程序生命周期

HTTP 模块ASP.NET 应用程序生命周期可通过 IHttpModule 类进行扩展.ASP.NET 包含若干实现 IHttpModule 的类,如 SessionStateModule 类.您还可以自行创建实现 IHttpModule 的类. 原文

Asp.Net的应用程序生命周期概述

参考文献: MSDN:Asp.Net应用程序生命周期 博客:选择HttpHandler还是HttpModule?

BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第8章节--配送SP2013Apps 应用程序生命周期

BEGINNING SHAREPOINT? 2013 DEVELOPMENT 第8章节--配送SP2013Apps 应用程序生命周期 你在商店拥有一个应用程序后,跟踪任何人们碰到的问题并发布更新以添加新功能,或者更改任何问题是非常重要的.任何生态系统中,一个流行的应用程序经常有负责人的开发人员,帮助客户解决问题,并发布更新更正常见问题.另外,客户喜欢购买应用程序,并希望看到随着日常更新和功能增强带来的价值增加.

应用程序生命周期的基本方法的转换

Android中生命周期方法有OnCreate().OnStart().OnReStart().OnReSume().OnPause().OnStop().OnDestroy(),一下是Activity的状态转换时相对应执行的方法的转换图 应用程序生命周期的基本方法的转换,布布扣,bubuko.com

图解ios程序生命周期

图解ios程序生命周期 应用程序启动后状态有Active.Inactive.Background.Suspended.Not running这5种状态,几种状态的转换见下图: 在AppDelegate中实现app状态变化时的回调函数,在 app状态发生变化时,系统会执行相应回调: - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions

[译]MVC应用程序生命周期

原文:MVC Application Lifecycle 来一探究竟在MVC应用程序中参与请求处理的各个不同组件. 目录: 序言 背景 UrlRoutingModule RouteHandler MvcHandler ControllerFactory Controller ActionInvoker ActionResult ViewEngine 总结 关注点 序言 在这篇文章中我们将讨论MVC应用程序生成周期以及当请求从一个组件传到另一个组件时是如何被处理的.我们将说说这些在应用程序生命周期