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. 精通移动app测试实战

  2. PL/SQL Developer-官网下载地址

    官网下载地址:https://www.allroundautomations.com/registered/plsqldev.html

  3. STL_算法_02_排序算法

    ◆ 常用的排序算法: 1.1.合并(容器A(全部/部分)&容器B(全部/部分)==>容器C(全部/部分),容器C中元素已经排好顺序),返回的值==>iteratorOutBegin ...

  4. 语言小知识-MySQL数据库引擎

    MySQL作为全世界广受欢迎的数据库,被用于很多中小型的项目中,但是你对 MySQL 数据库的存储引擎了解多少呢? 我们将逻辑表中的数据存储到数据库中,数据库又将我们表中的数据存储到物理设备中(如磁盘 ...

  5. Codeforces 918D - MADMAX

    918D - MADMAX 思路: dp+记忆化搜索 状态:dp[i][j][w]表示先手在i节点,后手在j节点,这一轮的字母为w的结果,如果为true,则表示先手必赢,否则后手必赢. 状态转移:如果 ...

  6. 算法笔记--KMP算法 && EXKMP算法

    1.KMP算法 这个博客写的不错:http://www.cnblogs.com/SYCstudio/p/7194315.html 模板: next数组的求解,那个循环本质就是如果相同前后缀不能加上该位 ...

  7. Codeforces 837D - Round Subset(dp)

    837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...

  8. XML和Schema

    2017-11-03 19:33:56 XML:Extensible Markup Language,也就是可扩展标记语言.XML工具使处理和转化信息变得十分容易和方便. XML和HTML格式是古老的 ...

  9. 雷林鹏分享:Ruby 语法

    Ruby 语法 让我们编写一个简单的 Ruby 程序.所有的 Ruby 文件扩展名都是 .rb.所以,把下面的源代码放在 test.rb 文件中. #!/usr/bin/ruby -w puts &q ...

  10. LeetCode--202--快乐数

    问题描述: 编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变 ...