UI程序的一般执行顺序:

先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做一次跳转,跳转至AppDelegate

UIApplicationMain() 函数的三大功能:

1.创建应用的UIApplication对象

2.指定应用程序的代理对象,代理的主要作用:监听应用程序是如何运行的.

3.建立事件循环(runloop:这个循环是一个死循环).作用:一旦用户操作应用程序,应用程序要立即做出响应,但用户操作应用的时间不确定,所以必须让代理一直处于监听状态,直到程序退出为止.

代码如下:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

而AppDelegate其实也是一个代理,它遵循UIApplicationDelegate协议,这就是为何当一开始AppDelegate里会有很多方法的原因.(重写UIApplicationDelegate协议里面必须实现的方法)

下面简要介绍这些方法:

1.告诉代理程序即将启动完成,程序准备运行

代理实现这个方法的时候,第一步创建一个window对象,第二步给window配置属性,第三步将window设置为主屏幕并可见,如果window上有其他视图,通过window呈现即可

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
lf.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

2.告诉代理程序将要进入非活跃状态(暂停,将timer时间类停止)

- (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. }

3.告诉代理应用程序已经进入后台(存储有用数据,释放一些内存等)

- (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.
}

4.告诉代理程序将要进入前台(取消暂停)

- (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.
}

5.告诉代理程序已经变得活跃(取消暂停状态的任务)

- (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.
}

6.

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

当系统提供的视图不满足开发实际要求的时候,这个时候就需要开发人员自定义视图.

常见的自定义视图是UILabel和UITextFiled一起组合起来

下面就以最简单的UILabel和UITextFiled组合的自定义视图为例说明:

首先新建一个类LTVIew

在LTVIew.h文件中,首先定义俩个属性UILabel和UITextField.

LTVIew.h

@interface LTVIew : UIView
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *textField;
@end

然后在LTVIew.h中重写initWithFrame:方法,实现对象一初始化就有UILabel和UITextField.

LTVIew.m

#import "LTVIew.h"

@implementation LTVIew
//重写UIView的方法
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
[self p_setUp];
}
return self;
}
//写完这个方法,要立即在重写父类的初始化方法里调用
-(void)p_setUp{ _label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width/3, self.frame.size.height)];
_label.backgroundColor=[UIColor yellowColor];
[self addSubview:_label];
[_label release]; _textField=[[UITextField alloc]initWithFrame:CGRectMake(_label.frame.size.width, 0, self.frame.size.width*2/3, self.frame.size.height)];
_textField.backgroundColor=[UIColor redColor];
[self addSubview:_textField];
[_textField release]; }
-(void)dealloc{
[_textField release];
[_label release];
[super dealloc];
}
@end

然后就可以在AppDelegate.m文件中创建自定义对象并对其赋值属性使用额.

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

UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏的更多相关文章

  1. shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏

    最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...

  2. UI基础:UITextField 分类: iOS学习-UI 2015-07-01 21:07 68人阅读 评论(0) 收藏

    UITextField 继承自UIControl,他是在UILabel基础上,对了文本的编辑.可以允许用户输入和编辑文本 UITextField的使用步骤 1.创建控件 UITextField *te ...

  3. UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏

    UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件 功能: 1.分担APPdelegate的工作 2.实现模块独立,能提高复用性 创建UIViewC ...

  4. 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏

    转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...

  5. Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏

    更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...

  6. SharedPreferences基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏

    见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...

  7. C语言基础:枚举.宏 分类: iOS学习 c语言基础 2015-06-10 22:01 20人阅读 评论(0) 收藏

    枚举:一组有符号的整型常量,一 一列举所有的状态 枚举常和switch连用 enum week{ monday=1, tuesday, wednesday, thursday, friday, sat ...

  8. 从Ecipse中导出程序至apk 分类: H1_ANDROID 2013-10-26 22:17 516人阅读 评论(0) 收藏

    若未有数字证书: 1. 2. 3. 4. 5. 若已有数字证书: 上面的后3步改为 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

随机推荐

  1. Croc Champ 2013 - Round 1 E. Copying Data 分块

    E. Copying Data time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. Codeforces 847B - Preparing for Merge Sort

    847B - Preparing for Merge Sort 思路:前面的排序的最后一个一定大于后面的排序的最后一个.所以判断要不要开始新的排序只要拿当前值和上一个排序最后一个比较就可以了. 代码: ...

  3. C#获取类库(DLL)的绝对路径

    C#中当我们在写公共的类库的时候难免会调用一些xml配置文件,而这个配置文件的路径则非常重要,常用的方式就是写在web.config中,而我们也可以将配置文件直接放在dll的同级目录,那么怎么获得当前 ...

  4. python3 操作windows的粘贴板(读取和传值)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import win32con import wi ...

  5. 20170517xlVBA添加数据透视表

    Sub AddPovitTable() 'Constance Const DATA_SHEET As String = "Advanced Filter" Const DATA_A ...

  6. Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level

    2018-03-23 18:32:21,690 [INFO] [http-nio-11007-exec-2] org.apache.coyote.http11.Http11Processor [Dir ...

  7. 体验异步的终极解决方案-ES7的Async/Await

    阅读本文前,期待您对promise和ES6(ECMA2015)有所了解,会更容易理解.本文以体验为主,不会深入说明,结尾有详细的文章引用. 第一个例子 Async/Await应该是目前最简单的异步方案 ...

  8. 未能加载文件或程序集“LinqToExcel”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“*”或它的某一个依赖项.试图加载格式不正确的程序. 原因:操作系统是64位的,但发布的程序引用了一些32位的ddl,所以出现了兼容性的问题解决方案一:如果是64位机器,IIS—— ...

  9. 062——VUE中vue-router之命名视图的实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. swiper中的默认值的属性和作用(小程序交流群:604788754)

    swiper中的重要属性: vertical:属性,控制swiper效果是水平切换滚动,还是垂直切换滚动.如果不设置此属性,默认是水平滚动,如果设置:vertical="true" ...