UIViewController生命周期控制-开发规范
从网上各位iOS们收集并总结:
各方法使用:
- init 中初始化一些UI组件,比如UIButton,UILabel等
- loadView 中
- createFields 接受参数,初始化变量
- createViews 创建视图
- createEvents 绑定事件,如按钮的点击,NotificationCenter,kvo等
- viewDidLoad
- loadData 加载数据,调用一些api
- dealloc(现在dealloc中做的事我放在了viewDidDisapper中)
- destroyEvents 取消事件的绑定
- destroyViews 释放,销毁视图
- destroyFields 释放,销毁引用的变量
- didReceiveMemoryWarning
- cleanData 释放一些可以释放的数据
- 额外
- enterForeground 进入前台时调用
- enterBackground 进入后台时调用
规范文件内部组织结构:
统一UIViewController风格,首先是头文件:
@interfaceUIViewController(base)
#pragma mark- model
// 定义model
#pragma mark- view
// 定义view
#pragma mark- api
// 定义api @end
#pragma mark - api
// 对外的接口
#pragma mark - rewrite
// 额外的重写的父类的方法
#pragma mark -private
//...
#pragma mark -响应 model 的地方
//...
#pragma mark 1 notification
//...
#pragma mark 2 KVO
//...
#pragma mark -响应 view 的地方
//...
#pragma mark 1 target-action
//...
#pragma mark 2delegate dataSource protocol
//...
#pragma mark -其他
//...
统一命名:
实现可以用runtime,也可以用基类,个人推荐是用基类,但是下面的代码是runtime的
@implementationUIViewController(base)
+(void)load {
XY_swizzleInstanceMethod([UIViewControllerclass],@selector(loadView),@selector(xy__loadView));
XY_swizzleInstanceMethod([UIViewControllerclass],@selector(viewDidLoad),@selector(xy__viewDidLoad));
XY_swizzleInstanceMethod([UIViewControllerclass],NSSelectorFromString(@"dealloc"),@selector(xy__dealloc));
XY_swizzleInstanceMethod([UIViewControllerclass],@selector(didReceiveMemoryWarning),@selector(xy__didReceiveMemoryWarning));
}
-(void) xy__loadView {
[self xy__loadView];
if([self respondsToSelector:@selector(createFields)])
[self performSelector:@selector(createFields)];
if([self respondsToSelector:@selector(createViews)])
[self performSelector:@selector(createViews)];
if([self respondsToSelector:@selector(enterBackground)]){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
if([self respondsToSelector:@selector(enterForeground)]){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
if([self respondsToSelector:@selector(createEvents)])
[self performSelector:@selector(createEvents)];
}
-(void)xy__dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
if([self respondsToSelector:@selector(destroyEvents)])
[self performSelector:@selector(destroyEvents)];
if([self respondsToSelector:@selector(destroyViews)])
[self performSelector:@selector(destroyViews)];
if([self respondsToSelector:@selector(destroyFields)])
[self performSelector:@selector(destroyFields)];
[self xy__dealloc];
}
-(void)xy__viewDidLoad {
if([self respondsToSelector:@selector(loadData)])
[self performSelector:@selector(loadData)];
[self xy__viewDidLoad];
}
-(void)xy__didReceiveMemoryWarning {
if([self isViewLoaded]&&[self.view window]== nil){
if([self respondsToSelector:@selector(cleanData)])
[self performSelector:@selector(cleanData)];
}
[self xy__didReceiveMemoryWarning];
}
@end- 欢迎拍砖指正。
UIViewController生命周期控制-开发规范的更多相关文章
- UIViewController生命周期控制
UIViewController生命周期控制 UIViewController介绍 官方的介绍例如以下 The UIViewController class provides the fundamen ...
- iOS UIViewController生命周期控制
具体流程,看下图: init方法在init方法中实例化必要的对象(遵从LazyLoad思想)init方法中初始化ViewController本身 loadView方法当view需要被展示而它却是nil ...
- 【iOS开发】iOS对UIViewController生命周期和属性方法的解析
iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...
- initWithFrame、initWithCoder、awakeFromNib的区别和调用次序 & UIViewController生命周期 查缺补漏
当我们创建或者自定义一个UI控件时,就很可能会调用awakeFromNib.initWithCoder .initWithFrame这些方法.三者的具体区别如下: initWithFrame: 通过代 ...
- iOS对UIViewController生命周期和属性方法的解析
目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...
- Vue.js 子组件的异步加载及其生命周期控制
前端开发社区的繁荣,造就了很多优秀的基于 MVVM 设计模式的框架,而组件化开发思想也越来越深入人心.这其中不得不提到 Vue.js 这个专注于 VM 层的框架. 本文主要对 Vue.js 组件化开发 ...
- Newbe.Claptrap 框架如何实现多级生命周期控制?
Newbe.Claptrap 框架如何实现多级生命周期控制?最近整理了一下项目的术语表.今天就谈谈什么是 Claptrap Lifetime Scope. 特别感谢 kotone 为本文提供的校对建议 ...
- UIViewController生命周期
UIViewController生命周期
- 你真的了解UIViewController生命周期吗?
一:首先了解一下生命周期图 二:UIViewController 生命周期介绍 1.通过alloc init 分配内存,初始化controller. 2.loadView loadView方法默认实现 ...
随机推荐
- SSIS中循环遍历组件[Foreach Loop Container]
背景 每月给业务部门提取数据,每个分公司都要提取一般,先跑SQL,再粘贴到Excel中,然后发邮件给相关的人员.费时费力,还容易粘贴错位.因此,需要通过一个程序完成这些步骤.我首先想到的是通过SSIS ...
- Mybatis 入门之resultMap与resultType解说实例
resultMap:适合使用返回值是自己定义实体类的情况 resultType:适合使用返回值得数据类型是非自己定义的,即jdk的提供的类型 resultMap : type:映射实体类的数据类型 i ...
- OR1200中指令Cache的结构
下面内容摘自<步步惊芯--软核处理器内部设计分析>一书 12.3 ICache结构 OR1200中实现ICache的文件有or1200_ic_top.v.or1200_ic_fsm.v.o ...
- 在dotnet core web api中支持CORS(跨域访问)
最近在写的Office add-in开发系列中,其中有一个比较共性的问题就是在add-in的客户端脚本中访问远程服务时,要特别注意跨域访问的问题. 关于CORS的一些基本知识,请参考维基百科的说明:h ...
- 在IDEA中实战Git(转载自)
转载自:http://blog.csdn.net/autfish/article/details/52513465 工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组 ...
- JavaScript基础2——关于变量
变量的声明 变量的定义:使用var关键字来声明,区分大小写的.注意:不用var,会污染全局变量. 变量的命名规范是:字母,数字,$符 ...
- Node.js前言
最近在学Node.js,所以学到一点东西就更新在上面吧,如果有错误,欢迎大家指正.
- Python学习日记:day8-------文件操作
文件操作 1,文件路径:d:\xxxx.txt 绝对路径:从根目录到最后 相对路径:当前目录下的文件 2,编码方式:utf-8 3,操作方式:只读,只写,追加,读写,写读...... ...
- java中的参数传递是按引用传递还是按值传递
最近去面试,有一个面试官问到java中参数传递的问题,感觉自己对于这一块还是理解的不够深.今天我们就一起来学习一下Java中的接口和抽象类.下面是本文的目录大纲: 一 . 什么是按值传递,什么是按引用 ...
- seo我告诉你
seo我告诉你,这回seo真的告诉你百度云链接 链接:http://pan.baidu.com/s/1qYpM9y8 密码:mad6 seo优化教程: