UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏
最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...
- UI基础:UITextField 分类: iOS学习-UI 2015-07-01 21:07 68人阅读 评论(0) 收藏
UITextField 继承自UIControl,他是在UILabel基础上,对了文本的编辑.可以允许用户输入和编辑文本 UITextField的使用步骤 1.创建控件 UITextField *te ...
- UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏
UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件 功能: 1.分担APPdelegate的工作 2.实现模块独立,能提高复用性 创建UIViewC ...
- 各种排序算法的分析及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 ...
- Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏
更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...
- SharedPreferences基础 分类: H1_ANDROID 2013-11-04 22:35 2559人阅读 评论(0) 收藏
见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...
- C语言基础:枚举.宏 分类: iOS学习 c语言基础 2015-06-10 22:01 20人阅读 评论(0) 收藏
枚举:一组有符号的整型常量,一 一列举所有的状态 枚举常和switch连用 enum week{ monday=1, tuesday, wednesday, thursday, friday, sat ...
- 从Ecipse中导出程序至apk 分类: H1_ANDROID 2013-10-26 22:17 516人阅读 评论(0) 收藏
若未有数字证书: 1. 2. 3. 4. 5. 若已有数字证书: 上面的后3步改为 版权声明:本文为博主原创文章,未经博主允许不得转载.
- sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏
sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...
随机推荐
- SVN基本命令总结
1.svn add [path] 预定添加文件或者目录到版本库,这些add的文件会在下一次提交文件时同步到版本服务器. 2.svn commit [path] 提交文件到版本服务器. 3.svn co ...
- Linux下wget下载整个FTP目录(含子目录)--转载
wget -nH -m --ftp-user=your_username --ftp-password=your_password ftp://your_ftp_host/* 解释:-nH:不创建以主 ...
- S4 对象系统
上一节中,我们介绍了 S3 系统.与大多数其他编程语言的面向对象系统不同,与那些类被定义为固定结构,且随着程序编译有确定的方法分派的系统相比,S3 系统显得非常不严谨.当我们定义一个 S3 类时,几乎 ...
- Linux访问windows共享(samba/smbclient/smbfs/cifs)
samba是一个实现不同操作系统之间文件共享和打印机共享的一种SMB协议的免费软件.●安装samba,samba-client和cifs-utils.x86_64此步将自动安装好相关依赖包:samba ...
- webpack 多页面支持 & 公共组件单独打包
webpack - 多页面/入口支持 & 公共组件单独打包 webpack系列目录 webpack 系列 一:模块系统的演进 webpack 系列 二:webpack 介绍&安装 we ...
- 关于C#中的日期的一个简单总结
首先,总结两个简单的方法,实现 秒 与 日期 的相互转换: public class MyTest { /// <summary> /// 将Unix时间戳转换为DateTime类型时间, ...
- 插件Vue.Draggable(5000🌟)
安装资源库:从Vue资源:https://github.com/vuejs/awesome-vue下载 Libraries/UI Components/Form/Drag and Drop yarn ...
- Liebig's Barrels CodeForces - 985C (贪心)
链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...
- 想3分钟搭建图像识别系统?这里有一份TensorFlow速成教程(转)
http://www.voidcn.com/article/p-wyaahqji-dr.html 从我们见到的各种图像识别软件来看,机器似乎能认出人脸.猫.狗.花草.各种汽车等等日常生活中出现的物体, ...
- 『PyTorch』第一弹_静动态图构建if逻辑对比
对比TensorFlow和Pytorch的动静态图构建上的差异 静态图框架设计好了不能够修改,且定义静态图时需要使用新的特殊语法,这也意味着图设定时无法使用if.while.for-loop等结构,而 ...