iOS复习笔记16:应用启动过程和工程结构

一 新建项目

打开Xcode->new->Project->iOS->Single View Application->下一步->输入工程名->下一步->选择路径->create

二 启动过程

1 载入程序到内存

2 在main函数中创建UIApplication

3 创建AppDelegate

4 开始主循环,监听事件

5 创建UIWindow,设置活动窗口

7 加载Info.plist文件,读取主storyboard文件

8 加载storyboard文件,创建ViewController

9 将上面创建的ViewController设置rootViewController

10 显示UIWindow,将rootViewController的view显示在UIWindow中

三 代码

// Application.h

...
// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no
// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.
UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
...

// main.m

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

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

// AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

// AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    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

// ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

// ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

时间: 2025-01-05 18:10:52

iOS复习笔记16:应用启动过程和工程结构的相关文章

openstack学习笔记一 虚拟机启动过程代码跟踪

本文主要通过对虚拟机创建过程的代码跟踪.观察虚拟机启动任务状态的变化,来透彻理解openstack各组件之间的作用过程. 当从horizon界面发送一个创建虚拟机请求,horizon api 将会依据前端给定的数据信息.调用novaclient 生成一个创建虚拟机的http post 请求来创建vm服务. >/usr/lib/python2.6/site-packages/horizon/api/nova.py(334)server_create() > /usr/lib/python2.6/

iOS复习笔记8:autorelease详解

一 概念 iOS在程序在运行的过程中,会创建很多个释放池,自动释放池以栈的形式存放的(先进后出). 对象调用autorelease时,会被放入栈顶的自动释放池中. 当自动释放池销毁时,会对池的所有对象发送一次release消息: 所以发送autorelease之后引用计数不会立即-1. autorelease返回对象本身. 二 实例 // Person.h @interface Person @property(nonatomic, assign) int age; @end // Person

iOS复习笔记1:HelloWorld项目

2012年10月到2013年5月期间,一直都做iOS开发,后来开始做用cocos做游戏开发. 过去了那么久,复习一下以前学的东西. 一 新建工程 打开Xcode,选择File>Project>Single View Application,然后输入工程名HelloWolrd,选择保存路径. 创建完成之后保存,如图: 二 运行 单击上图左上角的三角形按钮,程序久可是运行了,以下界面一闪而过: 然后出现: 这就是用模拟器运行程序啦:如果闲模拟器太大,看不清楚,可以点击模拟器,然后Window>

DSP2812学习笔记-内部Flash启动过程

2812从内部flash启动的详细流程说明: a) 程序硬件复位或者软件复位 b) 判断mp/mc是否为0,为0则从boot rom启动,否则从外部启动(见附录1) c) 到boot rom的0x3FFFC0处取出复位向量,跳到boot函数 d) 采集IO管脚状态,根据IO状态选择boot方式(见附录2) e) 如果是flash,程序退出boot函数,跳转到0x3F7FF6 f) 取出跳转指令,跳转到自己的指定地址或者C初始化的入口_C_INT00处 g)  在C初始化的入口_C_INT00对一

iOS复习笔记14:常用数据结构之类

一 NSString/NSMutableString字符串 1 NSString <pre name="code" class="objc">NSString* s1 = @"string"; //NSString* s2 = [[NSString alloc] initWithFormat(@"%d is one",1)]; NSString* s2 = [NSString stringWithFormat(@&

iOS复习笔记3:类的基本定义

// Car.h // 类的声明 // 类名:Car // 属性:m_nSpeed // 行为:run #import <Foundation/Foundation.h> // NSObject @interface Car : NSObject { // 属性:成员变量(可以是基础类型,枚举,结构体和类对象指针) @public int m_nSpeed;// 默认初始化为0 } // 行为:方法(方法名,返回值,参数) - (void)stop; - (void)run:(int)spee

iOS复习笔记13:常用结构体

都需要包含Foundation.h头文件 CG:CoreGraphics框架简称,CG开头的在Foundation框架中有声明,但是没有实现 NS:NextStep 一 NSRange范围 1 定义: typedef struct _NSRange{ NSUInteger location; NSUInteger length; }NSRange; 2 示例: NSString* str = @"I love OC"; // NSRange* rg = {2, 4}; // NSRan

iOS复习笔记15:NSObject

为了描述方便,把如下代码贴出来: @interface Student:NSObject -(void)go; -(void)showName:(NSString *)name; -(void)introduce:(NSString *)name :(NSString*)address; @end Student* stu = [[Student alloc]init]; 一 创建和初始化方法 alloc allocWithZone new copy -- 只有不可变到不可变是浅拷贝(相当于re

iOS复习笔记11:协议和代理

一 功能 可以在协议中声明方法(不能声明变量), 某个类只要遵守这个协议,就相当于拥有协议中所有的方法声明. 父类准守协议,子类也拥有协议中的方法. 协议也可以准守另一个协议. 基协议NSObject(同时也是基类),NSObject基类也准守基协议. 基协议中包含了常用的内存管理方法:release,retain方法等. 二 定义 1 协议定义 @protocal 协议名 <NSObject> // 声明方法 @end 2 遵守协议 2.1 类 @interface 类名 : 父类名 <