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方法默认实现 ...
随机推荐
- Progressive Web Apps入门
PC和Mobile开发技术演进 PC方向,从客户端到富客户端,到现在广泛使用的Web. 移动方向,目前主要还是原生应用和Mobile Web,PWA相关技术是未来发展方向. PWA的概念 ...
- 》》canvas时钟
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- iOS 获取导航栏和状态栏的高度
CGRect rect = [[UIApplication sharedApplication] statusBarFrame]; 状态栏的高度: float status height = rec ...
- JavaScript必知的特性(继承)
多数人在学习JavaScript的时候.都是做Web的时候.须要表单验证.或者是一些简单的DOM操作,如同我上篇所讲.处在一个"辅助"的地位. 处在"辅助"地位 ...
- 自己手写WEB程序框架并执行
1.新建目录,起名MyWeb 2.目录下,新建两个目录 WEB-INF, META-INF,,还能够新建一些jsp,html文件 ,如 index.html 3在WEB-INF中必须存在一个文件WEB ...
- js中常见的一些兼容性问题
1)滚动条: document.documentElement.scrollTop||document.body.scrollTop 2) 网页可视区域兼容 window.innerHeight || ...
- map对象拷贝问题
map对象赋值: HashMap<String,Object> hm = new HashMap(); HashMap<String,Object> hmCopy = new ...
- (五):C++分布式实时应用框架——支撑复杂的业务通讯关系
C++分布式实时应用框架--支撑复杂的业务通讯关系 技术交流合作QQ群:436466587 欢迎讨论交流 版权声明:本文版权及所用技术归属smartguys团队所有,对于抄袭,非经同意转载等行为保留法 ...
- 【MySQL】查看支持的引擎show engines;
- JavaScript闭包基本概念
闭包的概念 维基百科中是这么解释闭包的: 计算机科学中,闭包(也称为词法闭包或函数闭包)是指一个函数或函数的引用,与一个引用环境绑定在一起.这个函数环境是一个存储该函数每个非局部变量(也叫自由变量)的 ...