码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-04 09:41:37      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:ui   生命周期   执行过程   

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

创建第一个IOS应用程序的时候,我们一般创建一个IOS的single view application,程序的入口仍然是main函数,在main函数里调用了UIApplicationMain(argc, argv, nilNSStringFromClass([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


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

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

标签:ui   生命周期   执行过程   

原文地址:http://blog.csdn.net/pengjunlong123/article/details/46746443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!